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.keybox.manage.util.OTPUtil.java

/**
 * verifies code for OTP secret per time interval
 *
 * @param secret shared secret/*from   ww w  . j a v a2s.  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);

}

From source file:com.zimbra.common.auth.twofactor.TOTPAuthenticator.java

private byte[] decode(String secret, Encoding encoding) throws ServiceException {
    byte[] decoded;
    switch (encoding) {
    case BASE32:/*from w w w  .  j av  a 2s  .c  o m*/
        decoded = new Base32().decode(secret);
        break;
    case BASE64:
        decoded = Base64.decodeBase64(secret);
        break;
    default:
        throw ServiceException.FAILURE("unknown encoding", new Throwable());
    }
    return decoded;
}

From source file:com.torchmind.authenticator.AbstractTokenGenerator.java

/**
 * {@inheritDoc}/*from   w  w  w. j a v a 2  s  .  c  o  m*/
 */
@NonNull
@Override
public String buildHandshakeCode(@NonNull SecretKey secretKey, boolean humanReadable) {
    String code = (new Base32()).encodeAsString(secretKey.getEncoded());

    if (humanReadable) {
        String tmp = "";

        for (int i = 1; i <= code.length(); ++i) {
            tmp += code.charAt((i - 1));

            if ((i % 4) == 0) {
                tmp += " ";
            }
        }

        if (tmp.charAt((tmp.length() - 1)) == ' ') {
            code = tmp.substring(0, (tmp.length() - 1)).toLowerCase();
        } else {
            code = tmp.toLowerCase();
        }
    }

    return code;
}

From source file:com.threewks.thundr.util.Encoder.java

public Encoder base32() {
    data = new Base32().encode(data);
    return this;
}

From source file:aaf.vhr.crypto.GoogleAuthenticator.java

/**
 * Check the code entered by the user to see if it is valid
 * @param secret  The users secret.//  w ww  . j a v a2  s  . c  o  m
 * @param code  The code displayed on the users device
 * @param t  The time in msec (System.currentTimeMillis() for example)
 * @return
 */
public static boolean checkCode(String secret, long code, long timeMsec) {
    Base32 codec = new Base32();
    byte[] decodedKey = codec.decode(secret);

    // convert unix msec time into a 30 second "window"
    // this is per the TOTP spec (see the RFC for details)
    long t = (timeMsec / 1000L) / 30L;
    // Window is used to check codes generated in the near past.
    // You can use this value to tune how far you're willing to go.

    for (int i = -WINDOW_SIZE; i <= WINDOW_SIZE; ++i) {
        long hash;
        try {
            hash = verifyCode(decodedKey, t + i);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        if (hash == code) {
            return true;
        }
    }
    // The validation code is invalid.
    return false;
}

From source file:cz.alej.michalik.totp.server.User.java

/**
 * Ov TOTP kd//  www  .  j  a  v  a 2  s.  c  om
 * 
 * @param pass
 *            TOTP heslo poslan pomoc POST
 * @return JSON
 */
@Post
public String validate(String pass) {
    this.setStatus(error);
    msg.put("valid", "false");
    if (user != null && pass != null) {
        // Sdilene heslo
        byte[] secret = new Base32().decode(Data.get(user).getBytes());
        // Vygeneruji hesla v ?asovm rozsahu
        List<String> codes = getAllCodes(secret, 1);
        msg.put("status", "ok");
        if (codes.contains(pass)) {
            this.setStatus(success);
            msg.put("valid", "true");
        } else {
            this.setStatus(new Status(401));
            msg.put("valid", "false");
        }
    }
    return msg.toJSONString();

}

From source file:com.threewks.thundr.util.Encoder.java

public Encoder unbase32() {
    data = new Base32().decode(data);
    return this;
}

From source file:io.cos.cas.adaptors.postgres.models.OpenScienceFrameworkTimeBasedOneTimePassword.java

/**
 * Returns the TOTP secret in encoded as Base32.
 *
 * @return the encoded secret//from  w ww. ja  v  a  2 s  . co  m
 */
public String getTotpSecretBase32() {
    final byte[] bytes = DatatypeConverter.parseHexBinary(totpSecret);
    return new Base32().encodeAsString(bytes);
}

From source file:me.footlights.core.crypto.Fingerprint.java

public static Fingerprint decode(String algorithmName, String hash) throws NoSuchAlgorithmException {
    MessageDigest algorithm = MessageDigest.getInstance(algorithmName);
    final URI uri;
    try {/*www. j  a  v a 2  s . com*/
        uri = new URI("urn", algorithmName + ":" + hash, null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }

    return new Fingerprint(algorithm, ByteBuffer.wrap(new Base32().decode(hash.getBytes())), uri);
}

From source file:chronic.app.ChronicCookie.java

public static String createAuthCode(byte[] secret, String string, long value) throws GeneralSecurityException {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1");
    mac.init(signKey);//from   w ww  . ja  v a2s.co  m
    mac.update(string.getBytes());
    return new Base32().encodeAsString(mac.doFinal(Bytes.toByteArray(value)));
}