/** * Decodes a base32 String using the base32hex profile. Implementation * is case-insensitive and converts the given string to upper case before * decoding. * * @param data a base32 encoded String to decode. * @return the decoded String. */ public static byte[] decodeBase32(String data) { return data == null ? null : Base32Hex.decode(data.toUpperCase()); }
byte[] encBytes = new Base32().decode(addr.getBytes(StandardCharsets.UTF_8)); byte[] decBytes = encryptionSvc.decrypt(encBytes); addr = new String(decBytes, StandardCharsets.UTF_8).replaceAll("=", "");
@Test public void testBase32HexSamplesReverseLowercase() throws Exception { final Base32 codec = new Base32(true); for (final String[] element : BASE32HEX_TEST_CASES) { assertEquals(element[0], new String(codec.decode(element[1].toLowerCase()), CHARSET_UTF8)); } }
@Test public void testBase32HexSamplesReverse() throws Exception { final Base32 codec = new Base32(true); for (final String[] element : BASE32HEX_TEST_CASES) { assertEquals(element[0], new String(codec.decode(element[1]), CHARSET_UTF8)); } }
@Test public void testBase32BinarySamplesReverse() throws Exception { final Base32 codec = new Base32(); for (final Object[] element : BASE32_BINARY_TEST_CASES) { assertArrayEquals((byte[])element[0], codec.decode((String)element[1])); } }
private byte[] decodeSecret(String secret) { // Decoding the secret key to get its raw byte representation. switch (config.getKeyRepresentation()) { case BASE32: Base32 codec32 = new Base32(); // See: https://issues.apache.org/jira/browse/CODEC-234 // Commons Codec Base32::decode does not support lowercase letters. return codec32.decode(secret.toUpperCase()); case BASE64: Base64 codec64 = new Base64(); return codec64.decode(secret); default: throw new IllegalArgumentException("Unknown key representation type."); } }
/** * Decodes a base32 String using the base32hex profile. Implementation * is case-insensitive and converts the given string to upper case before * decoding. * * @param data a base32 encoded String to decode. * @return the decoded String. */ public static byte[] decodeBase32(String data) { return data == null ? null : Base32Hex.decode(data.toUpperCase()); }
/** * Decode a key from a base32 encoded string. * * @param key The encoded key string, expected ultimately have originated from {@link Key#toString()} * @return The resulting Key. */ public static Key fromString(String key) { if (key == null) { throw new IllegalArgumentException("Key must not be null"); } return new Key(base32.decode(key)); }
/** * BASE32 decoding helper (required for OCR representation) * * @param base32Data * BASE32 encoded data * @return binary representation of decoded data */ public static byte[] base32Decode(String base32Data) { Base32 decoder = new Base32(); return decoder.decode(base32Data); }
public Encoder unbase32() { data = new Base32().decode(data); return this; }
public Encoder unbase32() { data = new Base32().decode(data); return this; }
@Override public Map<byte[], byte[]> load(Path directory) throws IOException { Optional.ofNullable(directory) .filter(p -> p.toFile().isDirectory()) .orElseThrow(IllegalArgumentException::new); try (Stream<Path> stream = Files.list(directory)) { return stream.collect(Collectors.toMap( p -> new Base32().decode(p.toFile().getName()), p -> fileDelegate.readAllBytes(p))); } }
public static boolean verifyCode(String secret, int code, int variance) throws InvalidKeyException, NoSuchAlgorithmException { long timeIndex = System.currentTimeMillis() / 1000 / 30; byte[] secretBytes = new Base32().decode(secret); for (int i = -variance; i <= variance; i++) { if (getCode(secretBytes, timeIndex + i) == code) { return true; } } return false; }
public static final HOTPSecret fromString(String secret) { return new HOTPSecret((new Base32()).decode(secret.replace(" ", "").toUpperCase())); } }
private byte[] decodeSecret(String secret) { // Decoding the secret key to get its raw byte representation. switch (config.getKeyRepresentation()) { case BASE32: Base32 codec32 = new Base32(); // See: https://issues.apache.org/jira/browse/CODEC-234 // Commons Codec Base32::decode does not support lowercase letters. return codec32.decode(secret.toUpperCase()); case BASE64: Base64 codec64 = new Base64(); return codec64.decode(secret); default: throw new IllegalArgumentException("Unknown key representation type."); } }
public Entry (JSONObject jsonObj ) throws JSONException { this.setSecret(new Base32().decode(jsonObj.getString(JSON_SECRET))); this.setLabel(jsonObj.getString(JSON_LABEL)); }
private static long getCode(String secret, long timeIndex) throws InvalidKeyException, NoSuchAlgorithmException { return getCode(new Base32().decode(secret), timeIndex); }
/** * @param base32Key base32 encoded key * @param timeStepInMills * @return */ public static String getGeneratedValue(String base32Key, long timeStepInMills) { // time step String hexKey = new String(Hex.encode(new Base32().decode(base32Key))); return TOTP.generateTOTP(hexKey, Long.toHexString(System.currentTimeMillis() / timeStepInMills), "6", "HmacSHA1"); }
/** * 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); }
/** * 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); }