Search in sources :

Example 1 with Base32

use of org.apache.commons.codec.binary.Base32 in project KeyBox by skavanagh.

the class OTPUtil method generateSecret.

/**
     * generates OPT secret
     *
     * @return String shared secret
     */
public static String generateSecret() {
    byte[] buffer = new byte[(NUM_SCRATCH_CODES * SCRATCH_CODE_SIZE) + SECRET_SIZE];
    new SecureRandom().nextBytes(buffer);
    byte[] secret = Arrays.copyOf(buffer, SECRET_SIZE);
    return new String(new Base32().encode(secret));
}
Also used : SecureRandom(java.security.SecureRandom) Base32(org.apache.commons.codec.binary.Base32)

Example 2 with Base32

use of org.apache.commons.codec.binary.Base32 in project KeyBox by skavanagh.

the class OTPUtil method verifyToken.

/**
     * verifies code for OTP secret per time interval
     *
     * @param secret shared secret
     * @param token  verification token
     * @param time   time representation to calculate OTP
     * @return true if success
     */
private static boolean verifyToken(String secret, long token, long time) {
    long calculated = -1;
    byte[] key = new Base32().decode(secret);
    SecretKeySpec secretKey = new SecretKeySpec(key, "HmacSHA1");
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] hash = mac.doFinal(ByteBuffer.allocate(8).putLong(time).array());
        int offset = hash[hash.length - 1] & 0xF;
        for (int i = 0; i < 4; ++i) {
            calculated <<= 8;
            calculated |= (hash[offset + i] & 0xFF);
        }
        calculated &= 0x7FFFFFFF;
        calculated %= 1000000;
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return (calculated != -1 && calculated == token);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Base32(org.apache.commons.codec.binary.Base32) Mac(javax.crypto.Mac)

Example 3 with Base32

use of org.apache.commons.codec.binary.Base32 in project nem2-sdk-java by nemtech.

the class SecretLockTransaction method generateBytes.

@Override
byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    // Create Vectors
    int signatureVector = SecretLockTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = SecretLockTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = SecretLockTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = SecretLockTransactionBuffer.createFeeVector(builder, fee);
    int mosaicIdVector = SecretLockTransactionBuffer.createMosaicIdVector(builder, UInt64.fromBigInteger(mosaic.getId().getId()));
    int mosaicAmountVector = SecretLockTransactionBuffer.createMosaicAmountVector(builder, UInt64.fromBigInteger(mosaic.getAmount()));
    int durationVector = SecretLockTransactionBuffer.createDurationVector(builder, UInt64.fromBigInteger(duration));
    int secretVector = SecretLockTransactionBuffer.createSecretVector(builder, Hex.decode(secret));
    byte[] address = new Base32().decode(getRecipient().plain().getBytes(StandardCharsets.UTF_8));
    int recipientVector = SecretLockTransactionBuffer.createRecipientVector(builder, address);
    SecretLockTransactionBuffer.startSecretLockTransactionBuffer(builder);
    SecretLockTransactionBuffer.addSize(builder, 234);
    SecretLockTransactionBuffer.addSignature(builder, signatureVector);
    SecretLockTransactionBuffer.addSigner(builder, signerVector);
    SecretLockTransactionBuffer.addVersion(builder, version);
    SecretLockTransactionBuffer.addType(builder, getType().getValue());
    SecretLockTransactionBuffer.addFee(builder, feeVector);
    SecretLockTransactionBuffer.addDeadline(builder, deadlineVector);
    SecretLockTransactionBuffer.addMosaicId(builder, mosaicIdVector);
    SecretLockTransactionBuffer.addMosaicAmount(builder, mosaicAmountVector);
    SecretLockTransactionBuffer.addDuration(builder, durationVector);
    SecretLockTransactionBuffer.addHashAlgorithm(builder, hashType.getValue());
    SecretLockTransactionBuffer.addSecret(builder, secretVector);
    SecretLockTransactionBuffer.addRecipient(builder, recipientVector);
    int codedSecretLock = SecretLockTransactionBuffer.endSecretLockTransactionBuffer(builder);
    builder.finish(codedSecretLock);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger) Base32(org.apache.commons.codec.binary.Base32)

Example 4 with Base32

use of org.apache.commons.codec.binary.Base32 in project nem2-sdk-java by nemtech.

the class Base32Encoder method getBytes.

/**
 * 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);
}
Also used : Base32(org.apache.commons.codec.binary.Base32)

Example 5 with Base32

use of org.apache.commons.codec.binary.Base32 in project OpenAM by OpenRock.

the class AuthenticatorAppRegistrationURIBuilder method getAppRegistrationUri.

private String getAppRegistrationUri(OTPType otpType) throws DecoderException {
    String appRegistrationUri;
    byte[] secretPlainTextBytes = Hex.decodeHex(secretHex.toCharArray());
    Base32 base32 = new Base32();
    String secretBase32 = new String(base32.encode(secretPlainTextBytes));
    String userName = id.getName();
    String realm = extractHumanReadableRealmString(id.getRealm());
    appRegistrationUri = "otpauth://" + otpType.getIdentifier() + "/" + issuer + ":" + realm + userName + "?secret=" + secretBase32 + "&issuer=" + issuer + "&digits=" + codeLength;
    return appRegistrationUri;
}
Also used : Base32(org.apache.commons.codec.binary.Base32)

Aggregations

Base32 (org.apache.commons.codec.binary.Base32)7 FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)2 BigInteger (java.math.BigInteger)2 Mosaic (io.nem.sdk.model.mosaic.Mosaic)1 SecureRandom (java.security.SecureRandom)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1