本文整理汇总了Java中org.apache.commons.codec.binary.Base32类的典型用法代码示例。如果您正苦于以下问题:Java Base32类的具体用法?Java Base32怎么用?Java Base32使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Base32类属于org.apache.commons.codec.binary包,在下文中一共展示了Base32类的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);
}
}
示例2: makeTokenFromJSON
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
private static Token makeTokenFromJSON(JSONObject o) throws JSONException {
Token tmp = new Token(new Base32().decode(o.getString(SECRET)), o.getString(LABEL), o.getString(TYPE), o.getInt(DIGITS));
tmp.setAlgorithm(o.getString(ALGORITHM));
if (o.getString(TYPE).equals(HOTP)) {
tmp.setCounter(o.getInt(COUNTER));
}
if (o.getString(TYPE).equals(TOTP)) {
tmp.setPeriod(o.getInt(PERIOD));
}
if (o.optBoolean(WITHPIN, false)) {
tmp.setWithPIN(true);
tmp.setPin(o.getString(PIN));
tmp.setLocked(true);
}
if (o.optBoolean(TAPTOSHOW, false)) {
tmp.setWithTapToShow(true);
}
return tmp;
}
示例3: makeJSONfromToken
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
private static JSONObject makeJSONfromToken(Token t) throws JSONException {
JSONObject o = new JSONObject();
o.put(SECRET, new String(new Base32().encode(t.getSecret())));
o.put(LABEL, t.getLabel());
o.put(DIGITS, t.getDigits());
o.put(ALGORITHM, t.getAlgorithm());
o.put(TYPE, t.getType());
if (t.getType().equals(HOTP)) {
o.put(COUNTER, t.getCounter());
}
if (t.getType().equals(TOTP)) {
o.put(PERIOD, t.getPeriod());
}
if (t.isWithPIN()) {
o.put(WITHPIN, true);
o.put(PIN, t.getPin());
} else {
o.put(WITHPIN, false);
}
if (t.isWithTapToShow()) {
o.put(TAPTOSHOW, true);
}
return o;
}
示例4: JWTDecoder
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
parts = TokenUtils.splitToken(jwt);
final JWTParser converter = new JWTParser();
String headerJson = null;
String payloadJson = null;
switch (encodeType) {
case Base16:
headerJson = URLDecoder.decode(new String(Hex.decodeHex(parts[0])), "UTF-8");
payloadJson = URLDecoder.decode(new String(Hex.decodeHex(parts[1])), "UTF-8");
break;
case Base32:
Base32 base32 = new Base32();
headerJson = URLDecoder.decode(new String(base32.decode(parts[0]), "UTF-8"));
payloadJson = URLDecoder.decode(new String(base32.decode(parts[1]), "UTF-8"));
break;
case Base64:
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
break;
}
header = converter.parseHeader(headerJson);
payload = converter.parsePayload(payloadJson);
}
示例5: signBase32Encoding
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
private String signBase32Encoding() throws UnsupportedEncodingException{
Base32 base32 = new Base32();
String header = URLEncoder.encode(headerJson, "UTF-8");
String payload = URLEncoder.encode(payloadJson, "UTF-8");
byte[] bHeader = header.getBytes("UTF-8");
String encodedHeader = base32.encodeAsString(bHeader);
byte[] bPayload = payload.getBytes("UTF-8");
String encodedPayload = base32.encodeAsString(bPayload);
String content = String.format("%s.%s", encodedHeader, encodedPayload);
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
String signature = base32.encodeAsString(signatureBytes);
String signatureFinal = URLEncoder.encode(signature, "UTF-8");
return String.format("%s.%s", content, signatureFinal);
}
示例6: onCreate
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
credential = v.findCredentialByGUID(getArguments().getString(CREDENTIAL));
}
handler = new Handler();
otp_refresh = new Runnable() {
@Override
public void run() {
int progress = (int) (System.currentTimeMillis() / 1000) % 30 ;
otp_progress.setProgress(progress*100);
ObjectAnimator animation = ObjectAnimator.ofInt(otp_progress, "progress", (progress+1)*100);
animation.setDuration(1000);
animation.setInterpolator(new LinearInterpolator());
animation.start();
otp.setText(TOTPHelper.generate(new Base32().decode(credential.getOtp())));
handler.postDelayed(this, 1000);
}
};
}
示例7: encryptString
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
public static String encryptString(String password, String value) {
String salt = KeyGenerators.string().generateKey();
SecretKeySpec skeySpec = makeKeySpec(password, salt);
byte[] iv = KeyGenerators.secureRandom(16).generateKey();
String ivString = Hex.encodeHexString(iv);
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec,
new GCMParameterSpec(128, iv));
/*
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec,
new IvParameterSpec(iv));
*/
byte[] encrypted = cipher.doFinal(value.getBytes(Charset.forName("UTF-8")));
String s = StringUtils.strip(new Base32().encodeAsString(encrypted), "=").toLowerCase();
// Strip line breaks
s = salt + ivString + s.replaceAll("(\\n|\\r)", "");
return s;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例8: generateSharedSecret
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
/**
* Generates a random secret that is 16 characters long for the client which
* is encoded using Base32 according to RFC 3548.
*
* @return Base32 encoded shared Secret
* @throws OTPManagerException
* if a failure occur while generating the shared secret
*/
protected String generateSharedSecret() throws OTPManagerException {
LOG.info("Generating shared secret...");
int value = Constants.SECRET_BITS / 8 + Constants.SCRATCH_CODES * Constants.BYTES_PER_SCRATCH_CODE;
byte[] sharedSecret = null;
byte[] encodedSharedSecret = null;
Base32 codec = new Base32();
try {
secureRandom = SecureRandom.getInstance(Constants.RANDOM_NUMBER_ALGORITHM);
byte[] buffer = new byte[value];
secureRandom.nextBytes(buffer);
sharedSecret = Arrays.copyOf(buffer, Constants.SECRET_BITS / 8);
encodedSharedSecret = codec.encode(sharedSecret);
reSeed();
} catch (Exception e) {
LOG.error("Error while generating shared secret " + e.getMessage(), e);
throw new OTPManagerException("Error while generating shared secret " + e.getMessage(), e);
}
LOG.debug("Generated shared secret successfully");
return new String(encodedSharedSecret);
}
示例9: authCodeInternal
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
private int authCodeInternal(long time) throws NoSuchAlgorithmException, InvalidKeyException {
long t = time / 30000;
byte[] key = new Base32().decode(sharedSecret.toUpperCase().getBytes());
byte[] data = new byte[8];
long value = t;
int i = 8;
while (true) {
int i2 = i - 1;
if (i <= 0) {
break;
}
data[i2] = (byte) ((int) value);
value >>>= 8;
i = i2;
}
SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
byte[] hash = mac.doFinal(data);
int offset = hash[19] & 15;
long truncatedHash = 0;
for (int i2 = 0; i2 < 4; i2 += 1) {
truncatedHash = (truncatedHash << 8) | ((long) (hash[offset + i2] & 255));
}
return (int) ((truncatedHash & 2147483647L) % 1000000);
}
示例10: Entry
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
public Entry(String contents) throws Exception {
contents = contents.replaceFirst("otpauth", "http");
Uri uri = Uri.parse(contents);
URL url = new URL(contents);
if(!url.getProtocol().equals("http")){
throw new Exception("Invalid Protocol");
}
if(!url.getHost().equals("totp")){
throw new Exception();
}
String secret = uri.getQueryParameter("secret");
String label = uri.getPath().substring(1);
String issuer = uri.getQueryParameter("issuer");
if(issuer != null){
label = issuer +" - "+label;
}
this.label = label;
this.secret = new Base32().decode(secret.toUpperCase());
}
示例11: isValidOTP
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
public static boolean isValidOTP(String secret, int code) {
if (secret == null) {
return false;
} else {
byte[] buffer = new Base32().decode(secret);
long index = getTimeIndexOTP();
if (code == getCodeOTP(buffer, index - 2)) {
return true;
} else if (code == getCodeOTP(buffer, index - 1)) {
return true;
} else if (code == getCodeOTP(buffer, index)) {
return true;
} else if (code == getCodeOTP(buffer, index + 1)) {
return true;
} else if (code == getCodeOTP(buffer, index + 2)) {
return true;
} else {
return false;
}
}
}
示例12: 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;
}
示例13: 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();
}
示例14: 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;
}
示例15: verifyCode
import org.apache.commons.codec.binary.Base32; //导入依赖的package包/类
/**
* Verifies if a code is valid for a given secret. The code is valid for 90
* seconds. The current system time is used in the calculation.
*
* @param code
* The user supplied code to verify
* @param secret
* The shared secret between the mobile phone and the server
* @return true if the code is valid, false if the code is invalid
*/
public static boolean verifyCode(String code, String secret) {
try {
// Number of 30 second intervals since epoch
long timesteps = System.currentTimeMillis() / 1000L / 30L;
byte[] secretBytes = new Base32().decode(secret);
int codeInt = Integer.parseInt(code);
// A window where the code is valid
int window = 1;
for (int i = -window; i <= window; i++) {
if (hotp(secretBytes, timesteps + i) == codeInt) {
return true;
}
}
} catch (Exception e) {
// Catch all types of exceptions and return false
}
return false;
}