Example usage for org.apache.commons.codec.binary Base32 Base32

List of usage examples for org.apache.commons.codec.binary Base32 Base32

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base32 Base32.

Prototype

public Base32() 

Source Link

Usage

From source file:com.ec2box.manage.util.OTPUtil.java

/**
 * generates OPT secret//from w w w  .  ja v  a  2s. co  m
 *
 * @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));

}

From source file:crocserver.app.CrocSecurity.java

public static String createCode(byte[] secret, long value) throws Exception {
    Mac mac = createHmac(secret);
    return new Base32().encodeAsString(mac.doFinal(toByteArray(value)));
}

From source file:com.zxlim.totp.TOTP.java

public static final TOTP getInstance(final String encodedSecret) {
    Base32 base32 = new Base32();
    final byte[] secret = base32.decode(encodedSecret);
    return new TOTP(3, secret, encodedSecret);
}

From source file:me.whitmarbut.mfa.TOTP.java

private byte[] getSecretBytes(String secret) {
    secret = secret.toUpperCase();
    Base32 b32 = new Base32();
    return b32.decode(secret);
}

From source file:fr.ortolang.diffusion.security.authentication.TOTPHelper.java

public static String generateSecret() {
    byte[] buffer = new byte[20];
    rand.nextBytes(buffer);//from  w ww .  j  a  v  a2 s.  c om
    Base32 codec = new Base32();
    byte[] secretKey = Arrays.copyOf(buffer, 20);
    byte[] encodedKey = codec.encode(secretKey);
    return new String(encodedKey);
}

From source file:com.zxlim.totp.TOTP.java

public static final String generateSecret() {
    Base32 base32 = new Base32();
    SecureRandom random = null;/* w  w  w.  ja  va  2  s.  c  om*/
    final byte[] secret = new byte[secret_size_bits / 8];

    try {
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        return null;
    }

    random.nextBytes(secret);

    return new String(base32.encode(secret));
}

From source file:fr.ortolang.diffusion.security.authentication.TOTPHelper.java

private static int generateTOTP(String secret, long time) {
    Base32 codec = new Base32();
    byte[] decodedKey = codec.decode(secret);
    byte[] msg = ByteBuffer.allocate(8).putLong(time).array();
    byte[] hash = hmacSha(decodedKey, msg);
    int offset = hash[hash.length - 1] & 0xf;
    int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16)
            | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
    return binary % 1000000;
}

From source file:io.hops.hopsworks.common.dao.user.security.ua.SecurityUtils.java

/**
 * Generate the secrete key for mobile devices.
 *
 * @return/*from w  w  w.j  ava  2  s .co m*/
 */
public static String calculateSecretKey() throws NoSuchAlgorithmException {
    byte[] secretKey = new byte[10];
    SecureRandom sha1Prng = SecureRandom.getInstance("SHA1PRNG");
    sha1Prng.nextBytes(secretKey);
    Base32 codec = new Base32();
    byte[] encodedKey = codec.encode(secretKey);
    return new String(encodedKey);
}

From source file:com.ignorelist.kassandra.steam.scraper.FileCache.java

public FileCache(Path cacheDirectory, CacheLoader<String, ? extends InputStream> valueLoader,
        long expireAfterMillis) {
    codec = new Base32();
    stripedLock = Striped.lazyWeakReadWriteLock(4 * Runtime.getRuntime().availableProcessors());
    this.cacheDirectory = cacheDirectory;
    this.valueLoader = valueLoader;
    this.expireAfterMillis = expireAfterMillis;
}

From source file:com.ec2box.manage.util.OTPUtil.java

/**
 * verifies code for OTP secret per time interval
 *
 * @param secret shared secret//from www  .j  a v  a2 s  . c  o m
 * @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;

}