当前位置: 首页>>代码示例>>Java>>正文


Java Base32.decode方法代码示例

本文整理汇总了Java中org.apache.commons.codec.binary.Base32.decode方法的典型用法代码示例。如果您正苦于以下问题:Java Base32.decode方法的具体用法?Java Base32.decode怎么用?Java Base32.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.codec.binary.Base32的用法示例。


在下文中一共展示了Base32.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: verify

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
@Override
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
    byte[] signatureBytes = null;
    String signature = jwt.getSignature();
    String urlDecoded = null;
    switch (encodeType) {
        case Base16:
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = Hex.decodeHex(urlDecoded);
            break;
        case Base32:
            Base32 base32 = new Base32();
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = base32.decode(urlDecoded);
            break;
        case Base64:
            signatureBytes = Base64.decodeBase64(signature);
            break;
    }
    if (signatureBytes.length > 0) {
        throw new SignatureVerificationException(this);
    }
}
 
开发者ID:GJWT,项目名称:javaOIDCMsg,代码行数:24,代码来源:NoneAlgorithm.java

示例2: checkCode

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
public static boolean checkCode(String secret, long code, long timeMsec) {
  Base32 codec = new Base32();
  byte[] decodedKey = codec.decode(secret);
  long t = (timeMsec / 1000L) / 30L;
  for (int i = -WINDOW_SIZE; i <= WINDOW_SIZE; ++i) {
    long hash;
    try {
      hash = computeHash(decodedKey, t + i);
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e.getMessage());
    }
    if (hash == code) {
      return true;
    }
  }
  return false;
}
 
开发者ID:WeAreWizards,项目名称:passopolis-server,代码行数:19,代码来源:TwoFactorCodeChecker.java

示例3: twoFactorTestSetup

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
/** Enables two-factor auth for testIdentity, storing parameters in twoFactorData. */
@Before
public void twoFactorTestSetup() throws Exception {
  twoFactorData = new TwoFactorTestData();

  // set secret in DB means two factor auth is enabled
  testIdentity.setTwoFactorSecret(twoFactorData.testSecret);

  // create testToken and sign it
  twoFactorData.testToken =
      GetMyPrivateKey.makeLoginTokenString(testIdentity, twoFactorData.redirectUrl, null);
  twoFactorData.testSignature = TwoFactorSigningService.signToken(twoFactorData.testToken);

  // create code as if the google authenticator had
  Base32 codec = new Base32();
  byte[] decodedKey = codec.decode(twoFactorData.testSecret);
  long t = (System.currentTimeMillis() / 1000L) / 30L;
  twoFactorData.validTimeCode = Integer.toString(TwoFactorCodeChecker.computeHash(decodedKey, t));
  twoFactorData.backupCode = "123456789";
  byte[] salt = CryptoForBackupCodes.randSaltGen();
  testIdentity.setBackup(0, CryptoForBackupCodes.digest(twoFactorData.backupCode, salt));

  manager.identityDao.update(testIdentity);
  manager.commitTransaction();
}
 
开发者ID:WeAreWizards,项目名称:passopolis-server,代码行数:26,代码来源:TwoFactorTests.java

示例4: checkCode

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
/**
 * This method implements the algorithm specified in RFC 6238 to check if a
 * validation code is valid in a given instant of time for the given secret
 * key.
 *
 * @param secret    the Base32 encoded secret key.
 * @param codeString      the code to validate.
 * @return <code>true</code> if the validation code is valid,
 * <code>false</code> otherwise.
 */
public static boolean checkCode(final String secret, final String codeString) {
    final long code;
    try {
        code = Long.parseLong(codeString);
    } catch(final NumberFormatException e) {
        return false;
    }
    final Base32 codec32 = new Base32();
    final byte[] decodedKey = codec32.decode(secret);
    final long timeWindow = System.currentTimeMillis() / 30000;
    final int window = 0;
    for (int i = -((window - 1) / 2); i <= window / 2; ++i) {
        final long hash = calculateCode(decodedKey, timeWindow + i);
        if (hash == code) {
            return true;
        }
    }
    return false;
}
 
开发者ID:bubblecloud,项目名称:ilves,代码行数:30,代码来源:GoogleAuthenticatorService.java

示例5: verify

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
@Override
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
    byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
    byte[] signatureBytes = null;
    String signature = jwt.getSignature();
    String urlDecoded = null;
    switch (encodeType) {
        case Base16:
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = Hex.decodeHex(urlDecoded);
            break;
        case Base32:
            Base32 base32 = new Base32();
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = base32.decode(urlDecoded);
            break;
        case Base64:
            signatureBytes = Base64.decodeBase64(signature);
            break;
    }

    try {
        ECPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
        if (publicKey == null) {
            throw new IllegalStateException("The given Public Key is null.");
        }
        boolean valid = crypto.verifySignatureFor(getDescription(), publicKey, contentBytes, JOSEToDER(signatureBytes));

        if (!valid) {
            throw new SignatureVerificationException(this);
        }
    } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
        throw new SignatureVerificationException(this, e);
    }
}
 
开发者ID:GJWT,项目名称:javaOIDCMsg,代码行数:36,代码来源:ECDSAAlgorithm.java

示例6: verify

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
@Override
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
    byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
    byte[] signatureBytes = null;
    String signature = jwt.getSignature();
    String urlDecoded = null;
    switch (encodeType) {
        case Base16:
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = Hex.decodeHex(urlDecoded);
            break;
        case Base32:
            Base32 base32 = new Base32();
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = base32.decode(urlDecoded);
            break;
        case Base64:
            signatureBytes = Base64.decodeBase64(signature);
            break;
    }

    try {
        boolean valid = crypto.verifySignatureFor(getDescription(), secret, contentBytes, signatureBytes);
        if (!valid) {
            throw new SignatureVerificationException(this);
        }
    } catch (IllegalStateException | InvalidKeyException | NoSuchAlgorithmException e) {
        throw new SignatureVerificationException(this, e);
    }
}
 
开发者ID:GJWT,项目名称:javaOIDCMsg,代码行数:31,代码来源:HMACAlgorithm.java

示例7: verify

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
@Override
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
    byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
    byte[] signatureBytes = null;
    String signature = jwt.getSignature();
    String urlDecoded = null;
    switch (encodeType) {
        case Base16:
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = Hex.decodeHex(urlDecoded);
            break;
        case Base32:
            Base32 base32 = new Base32();
            urlDecoded = URLDecoder.decode(signature, "UTF-8");
            signatureBytes = base32.decode(urlDecoded);
            break;
        case Base64:
            signatureBytes = Base64.decodeBase64(signature);
            break;
    }

    try {
        RSAPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
        if (publicKey == null) {
            throw new IllegalStateException("The given Public Key is null.");
        }
        boolean valid = crypto.verifySignatureFor(getDescription(), publicKey, contentBytes, signatureBytes);
        if (!valid) {
            throw new SignatureVerificationException(this);
        }
    } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
        throw new SignatureVerificationException(this, e);
    }
}
 
开发者ID:GJWT,项目名称:javaOIDCMsg,代码行数:35,代码来源:RSAAlgorithm.java

示例8: doDecryptString

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
private static String doDecryptString(String password, String encryptedBase32) throws Exception {
    encryptedBase32 = StringUtils.strip(encryptedBase32, "=");
    String salt = encryptedBase32.substring(0, 16);
    String ivString = encryptedBase32.substring(16, 48);
    byte[] iv = new byte[0];
    try {
        iv = Hex.decodeHex(ivString.toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
    encryptedBase32 = encryptedBase32.substring(48);
    Base32 decoder = new Base32();
    byte[] encrypted = decoder.decode(encryptedBase32.toUpperCase());
    SecretKeySpec skeySpec = makeKeySpec(password, salt);



        /*
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec,
                new IvParameterSpec(iv));
          */
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec,
            new GCMParameterSpec(128, iv));

    byte[] original = cipher.doFinal(encrypted);
    return new String(original, Charset.forName("UTF-8"));

}
 
开发者ID:StallionCMS,项目名称:stallion-core,代码行数:31,代码来源:Encrypter.java

示例9: getTOTPCode

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
public static String getTOTPCode(String secretKey) {
    String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase();
    Base32 base32 = new Base32();
    byte[] bytes = base32.decode(normalizedBase32Key);
    String hexKey = Hex.encodeHexString(bytes);
    long time = (System.currentTimeMillis() / 1000) / 30;
    String hexTime = Long.toHexString(time);
    return TOTP.generateTOTP(hexKey, hexTime, "6");
}
 
开发者ID:asaph,项目名称:twofactorauth,代码行数:10,代码来源:GoogleAuthenticatorDemo.java

示例10: ReplicatorItem

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
public ReplicatorItem(String secret_key, String name_deliver, String name_account, String digits) {
    this.name_deliver = name_deliver;
    Base32 base32 = new Base32();
    this.secret_key = base32.decode(secret_key.replaceAll("\\s+", "").toUpperCase().getBytes());
    this.name_account = name_account;
    this.digits = Integer.parseInt(digits);
}
 
开发者ID:JeanBarriere,项目名称:Replicator,代码行数:8,代码来源:ReplicatorItem.java

示例11: testDecodeBase32

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
@Test
public void testDecodeBase32() {
	Random random = new Random();
	random.nextBytes(new byte[100]);
	Base32 base32 = new Base32();
	for (int i = 0; i < 10000; i++) {
		byte[] bytes = new byte[random.nextInt(10) + 1];
		random.nextBytes(bytes);
		String encoded = base32.encodeAsString(bytes);
		byte[] expected = base32.decode(encoded);
		byte[] actual = TimeBasedOneTimePasswordUtil.decodeBase32(encoded);
		assertArrayEquals(expected, actual);
	}
}
 
开发者ID:j256,项目名称:two-factor-auth,代码行数:15,代码来源:TwoFactorAuthUtilTest.java

示例12: getBytes

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
/**
 * Converts a string to a byte array.
 *
 * @param base32String The input Base32 string.
 * @return The output byte array.
 */
public static byte[] getBytes(final String base32String) {
	final Base32 codec = new Base32();
	final byte[] encodedBytes = StringEncoder.getBytes(base32String);
	if (!codec.isInAlphabet(encodedBytes, true)) {
		throw new IllegalArgumentException("malformed base32 string passed to getBytes");
	}

	return codec.decode(encodedBytes);
}
 
开发者ID:NemProject,项目名称:nem.core,代码行数:16,代码来源:Base32Encoder.java

示例13: decodeCheck

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
protected static byte[] decodeCheck(VersionByte versionByte, char[] encoded) {
    byte[] bytes = new byte[encoded.length];
    for (int i = 0; i < encoded.length; i++) {
        if (encoded[i] > 127) {
            throw new IllegalArgumentException("Illegal characters in encoded char array.");
        }
        bytes[i] = (byte) encoded[i];
    }

    Base32 base32Codec = new Base32();
    byte[] decoded = base32Codec.decode(bytes);
    byte decodedVersionByte = decoded[0];
    byte[] payload  = Arrays.copyOfRange(decoded, 0, decoded.length-2);
    byte[] data     = Arrays.copyOfRange(payload, 1, payload.length);
    byte[] checksum = Arrays.copyOfRange(decoded, decoded.length-2, decoded.length);

    if (decodedVersionByte != versionByte.getValue()) {
        throw new FormatException("Version byte is invalid");
    }

    byte[] expectedChecksum = StrKey.calculateChecksum(payload);

    if (!Arrays.equals(expectedChecksum, checksum)) {
        throw new FormatException("Checksum invalid");
    }

    if (VersionByte.SEED.getValue() == decodedVersionByte) {
        Arrays.fill(bytes, (byte) 0);
        Arrays.fill(decoded, (byte) 0);
        Arrays.fill(payload, (byte) 0);
    }

    return data;
}
 
开发者ID:stellar,项目名称:java-stellar-sdk,代码行数:35,代码来源:StrKey.java

示例14: getOneTimePassword

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
/**
 * @param secret
 * @param lengthToken
 * @return
 */
public static long getOneTimePassword(String secret, int lengthToken) {
	HorusTOTP.setSecretKey(secret);
	Base32 codec = new Base32();
	byte[] data = new byte[8];
	long value = (new Date().getTime()) / TimeUnit.SECONDS.toMillis(30);
	for (int i = 8; i-- > 0; value >>>= 8) {
		data[i] = (byte) value;
	}
	SecretKeySpec signKey = new SecretKeySpec(codec.decode(secretKey), "HmacSHA1");
	try {
		Mac mac = Mac.getInstance("HmacSHA1");
		mac.init(signKey);
		byte[] hash = mac.doFinal(data);
		int offset = hash[20 - 1] & 0xF;
		long truncatedHash = 0;
		for (int i = 0; i < 4; ++i) {
			truncatedHash <<= 8;
			truncatedHash |= (hash[offset + i] & 0xFF);
		}
		truncatedHash &= 0x7FFFFFFF;
		truncatedHash %= 1000 * 1000;
		return truncatedHash;
	} catch (Exception e) {
		e.printStackTrace();
		return 0;
	}
}
 
开发者ID:miamarti,项目名称:HorusFramework,代码行数:33,代码来源:HorusTOTP.java

示例15: checkCode

import org.apache.commons.codec.binary.Base32; //导入方法依赖的package包/类
/**
 * @param secret
 * @param code
 * @param window
 * @return
 */
private boolean checkCode(String secret, long code, int window) {
	Base32 codec = new Base32();
	byte[] decodedKey = codec.decode(secret);
	final long timeWindow = (new Date().getTime()) / TimeUnit.SECONDS.toMillis(30);
	for (int i = -((window - 1) / 2); i <= window / 2; ++i) {
		long hash = calculateCode(decodedKey, timeWindow + i);
		if (hash == code) {
			return true;
		}
	}
	return false;
}
 
开发者ID:miamarti,项目名称:HorusFramework,代码行数:19,代码来源:HorusTOTP.java


注:本文中的org.apache.commons.codec.binary.Base32.decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。