List of usage examples for org.apache.commons.codec.binary Base32 Base32
public Base32()
From source file:org.callimachusproject.xproc.DeserializeCascadingStyleSheetStep.java
private String decodeText(XdmNode doc) throws UnsupportedEncodingException, DecoderException { String text = extractText(doc); if ("base64".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); }//w ww.jav a 2 s.c o m byte[] decoded = Base64.decodeBase64(text); return new String(decoded, charset); } else if ("base32".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); } byte[] decoded = new Base32().decode(text); return new String(decoded, charset); } else if ("hex".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); } byte[] decoded = Hex.decodeHex(text.toCharArray()); return new String(decoded, charset); } else if ("binary".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); } byte[] decoded = BinaryCodec.fromAscii(text.toCharArray()); return new String(decoded, charset); } else if ("quoted-printable".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); } return new QuotedPrintableCodec(charset).decode(text); } else if ("www-form-urlencoded".equals(encoding)) { if (charset == null) { throw XProcException.stepError(10); } return new URLCodec(charset).decode(text); } else if (encoding != null && encoding.length() != 0) { throw new XProcException(step.getNode(), "Unexpected encoding: " + encoding); } else { return text; } }
From source file:org.exfio.weave.account.exfiopeer.ExfioPeerV1.java
private String generateAuthCode(int chars) { SecureRandom rnd = new SecureRandom(); Base32 b32codec = new Base32(); int bytes = (int) Math.ceil((double) chars * 5 / 8); String authCode = b32codec.encodeToString(rnd.generateSeed(bytes)); // Convert to uppercase, translate L and O to 8 and 9 authCode = authCode.toUpperCase().replace('L', '8').replace('O', '9').replaceAll("=", ""); //Return the specified number of chars only return authCode.substring(0, chars - 1); }
From source file:org.grycap.gpf4med.util.NamingUtils.java
/** * Joins the tokens and dates provided as parameters, computes a digest and returns a Base32 * representation of the string produced that can be used to identify a cache element. The * generated key will only contain the characters A-Z and 2-7. * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC4648</a> * @param tokens The list of input tokens that have to be used to compute the key. * @param dates The list of input dates that have to be used to compute the key. * @param extension Optional file-name extension. * @return A Base32 representation of the computed file-name. * @throws IOException is thrown if the computation of the file-name fails. *//*www . jav a 2 s . c om*/ public static String genSafeFilename(final @Nullable String[] tokens, final @Nullable Date[] dates, final @Nullable String extension) throws IOException { try { final byte[] digest = digest(tokens, dates); // encode with Base32 return new Base32().encodeAsString(digest) + (StringUtils.isNotBlank(extension) ? extension.trim() : ""); } catch (Exception e) { throw new IOException("Filename generation has failed", e); } }
From source file:org.gtri.totp.java
/** Gen Shared Secret * - TOTP relies on a 10 byte shared secret encoded as a 16 character base32 string *///from www . j a v a2s . c om public String genSharedSecret() { byte[] bBuffer = new byte[10]; new SecureRandom().nextBytes(bBuffer); log.debug("Generating Base32 TOTP Shared Secret"); return new String(new Base32().encode(bBuffer)); }
From source file:org.gtri.totp.java
/** getCode * Calculates the code for the provided time and shared secret. */// w w w . j a v a 2 s. co m public int getCode(String sharedSecret, long time) throws NoSuchAlgorithmException, InvalidKeyException { byte[] secret = new Base32().decode(sharedSecret); SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1"); ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(time); byte[] timeBytes = buffer.array(); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] hash = mac.doFinal(timeBytes); int offset = hash[19] & 0xf; long truncatedHash = hash[offset] & 0x7f; for (int i = 1; i < 4; i++) { truncatedHash <<= 8; truncatedHash |= hash[offset + i] & 0xff; } return (int) (truncatedHash %= 1000000); }
From source file:org.jahia.security.TOTP.java
public static boolean check_code(String secret, long code, long t) throws NoSuchAlgorithmException, InvalidKeyException { Base32 codec = new Base32(); byte[] decodedKey = codec.decode(secret); // 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. int window = 3; for (int i = -window; i <= window; ++i) { long hash = verify_code(decodedKey, t + i); if (hash == code) { return true; }/* w w w .jav a2 s . c o m*/ } // The validation code is invalid. return false; }
From source file:org.lib4j._2fa.GoogleAuthenticator.java
public static String getRandomSecretKey() { final byte[] bytes = new byte[20]; new SecureRandom().nextBytes(bytes); return new Base32().encodeToString(bytes); }
From source file:org.lib4j._2fa.GoogleAuthenticator.java
/** * @param secretKey Base32 encoded secret key (will be converted to * upper-case, and may have optional whitespace which will be removed) * @return The TOTP code for the secret key. *///from ww w .j a va 2 s . c o m public static String getTOTPCode(final String secretKey) { final String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase(); final byte[] bytes = new Base32().decode(normalizedBase32Key); final long time = (System.currentTimeMillis() / 1000) / 30; final String hexTime = Long.toHexString(time); return TOTP.generateTOTP(Hex.encodeHexString(bytes), hexTime, "6"); }
From source file:org.mozilla.android.sync.crypto.Cryptographer.java
public static String sha1Base32(String utf8) throws NoSuchAlgorithmException, UnsupportedEncodingException { return new Base32().encodeAsString(sha1(utf8)).toLowerCase(); }
From source file:org.mozilla.android.sync.crypto.Utils.java
/** * Decode a friendly base32 string.//from ww w .ja v a2 s .c om * see http://docs.services.mozilla.com/sync/storageformat5.html#the-sync-key * * @param base32 a friendly base32 string * @return the bytes of the encoded string */ public static byte[] decodeFriendlyBase32(String base32) { Base32 converter = new Base32(); return converter.decode(base32.replace('8', 'l').replace('9', 'o').replaceAll("-", "").toUpperCase()); }