@Override protected String getEncodedValue(String value, boolean omitPadding) { BaseEncoding encoding = BaseEncoding.base32Hex(); encoding = omitPadding ? encoding.omitPadding() : encoding; return encoding.encode(value.getBytes(StandardCharsets.UTF_8)); }
@Override protected String getEncodedValue(String value, boolean omitPadding) { BaseEncoding encoding = BaseEncoding.base32Hex(); encoding = omitPadding ? encoding.omitPadding() : encoding; return new String(encoding.decode(value), StandardCharsets.UTF_8); }
public void testBase32HexLenientPadding() { testDecodes(base32Hex(), "CPNMU", "foo"); testDecodes(base32Hex(), "CPNMU=", "foo"); testDecodes(base32Hex(), "CPNMU==", "foo"); testDecodes(base32Hex(), "CPNMU===", "foo"); // proper padding length testDecodes(base32Hex(), "CPNMU====", "foo"); testDecodes(base32Hex(), "CPNMU=====", "foo"); }
public void testBase32Hex() { // The following test vectors are specified in RFC 4648 itself testEncodingWithCasing(base32Hex(), "", ""); testEncodingWithCasing(base32Hex(), "f", "CO======"); testEncodingWithCasing(base32Hex(), "fo", "CPNG===="); testEncodingWithCasing(base32Hex(), "foo", "CPNMU==="); testEncodingWithCasing(base32Hex(), "foob", "CPNMUOG="); testEncodingWithCasing(base32Hex(), "fooba", "CPNMUOJ1"); testEncodingWithCasing(base32Hex(), "foobar", "CPNMUOJ1E8======"); }
public void testBase32HexInvalidDecodings() { // These contain bytes not in the decodabet. assertFailsToDecode(base32Hex(), "A\u007f", "Unrecognized character: 0x7f"); assertFailsToDecode(base32Hex(), "Wf2!", "Unrecognized character: W"); // This sentence just isn't base32 encoded. assertFailsToDecode(base32Hex(), "let's not talk of love or chains!"); // An 8n+{1,3,6} length string is never legal base32. assertFailsToDecode(base32Hex(), "A"); assertFailsToDecode(base32Hex(), "ABC"); assertFailsToDecode(base32Hex(), "ABCDEF"); }
@GwtIncompatible // Reader/Writer public void testBase32HexStreaming() throws IOException { // The following test vectors are specified in RFC 4648 itself testStreamingEncodingWithCasing(base32Hex(), "", ""); testStreamingEncodingWithCasing(base32Hex(), "f", "CO======"); testStreamingEncodingWithCasing(base32Hex(), "fo", "CPNG===="); testStreamingEncodingWithCasing(base32Hex(), "foo", "CPNMU==="); testStreamingEncodingWithCasing(base32Hex(), "foob", "CPNMUOG="); testStreamingEncodingWithCasing(base32Hex(), "fooba", "CPNMUOJ1"); testStreamingEncodingWithCasing(base32Hex(), "foobar", "CPNMUOJ1E8======"); }
public void testToString() { assertEquals("BaseEncoding.base64().withPadChar('=')", base64().toString()); assertEquals("BaseEncoding.base32Hex().omitPadding()", base32Hex().omitPadding().toString()); assertEquals( "BaseEncoding.base32().lowerCase().withPadChar('$')", base32().lowerCase().withPadChar('$').toString()); assertEquals( "BaseEncoding.base16().withSeparator(\"\n\", 10)", base16().withSeparator("\n", 10).toString()); } }
/** * Generates an unique random io. * * @return a random id in base 32 encoding */ public String generateId() { byte[] rndBytes = new byte[16]; ThreadLocalRandom.current().nextBytes(rndBytes); return BaseEncoding.base32Hex().encode(rndBytes).replace("=", ""); }
@Override public String getReference(@Nonnull String blobId) { checkNotNull(blobId, "BlobId must be specified"); try { Mac mac = Mac.getInstance(ALGORITHM); mac.init(new SecretKeySpec(getReferenceKey(), ALGORITHM)); byte[] hash = mac.doFinal(blobId.getBytes("UTF-8")); return blobId + ':' + BaseEncoding.base32Hex().encode(hash); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (InvalidKeyException e) { throw new IllegalStateException(e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
@Override public String getReference(@NotNull String blobId) { checkNotNull(blobId, "BlobId must be specified"); try { Mac mac = Mac.getInstance(ALGORITHM); mac.init(new SecretKeySpec(getReferenceKey(), ALGORITHM)); byte[] hash = mac.doFinal(blobId.getBytes("UTF-8")); return blobId + ':' + BaseEncoding.base32Hex().encode(hash); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (InvalidKeyException e) { throw new IllegalStateException(e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
@Override public String getReference(@NotNull String blobId) { checkNotNull(blobId, "BlobId must be specified"); try { Mac mac = Mac.getInstance(ALGORITHM); mac.init(new SecretKeySpec(getReferenceKey(), ALGORITHM)); byte[] hash = mac.doFinal(blobId.getBytes("UTF-8")); return blobId + ':' + BaseEncoding.base32Hex().encode(hash); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (InvalidKeyException e) { throw new IllegalStateException(e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
private boolean tokenMatchesHmac(String token, String currentHmac) { final String hmacCalculatedFromToken = BaseEncoding.base32Hex() .lowerCase().omitPadding() .encode(HmacUtils.hmacSha1(apiKeyHmacSecret, token)); return hmacCalculatedFromToken.equals(currentHmac); } }
public void testBase32Hex() { // The following test vectors are specified in RFC 4648 itself testEncodingWithCasing(base32Hex(), "", ""); testEncodingWithCasing(base32Hex(), "f", "CO======"); testEncodingWithCasing(base32Hex(), "fo", "CPNG===="); testEncodingWithCasing(base32Hex(), "foo", "CPNMU==="); testEncodingWithCasing(base32Hex(), "foob", "CPNMUOG="); testEncodingWithCasing(base32Hex(), "fooba", "CPNMUOJ1"); testEncodingWithCasing(base32Hex(), "foobar", "CPNMUOJ1E8======"); }
public void testBase32HexLenientPadding() { testDecodes(base32Hex(), "CPNMU", "foo"); testDecodes(base32Hex(), "CPNMU=", "foo"); testDecodes(base32Hex(), "CPNMU==", "foo"); testDecodes(base32Hex(), "CPNMU===", "foo"); // proper padding length testDecodes(base32Hex(), "CPNMU====", "foo"); testDecodes(base32Hex(), "CPNMU=====", "foo"); }
public void testBase32HexInvalidDecodings() { // These contain bytes not in the decodabet. assertFailsToDecode(base32Hex(), "A\u007f", "Unrecognized character: 0x7f"); assertFailsToDecode(base32Hex(), "Wf2!", "Unrecognized character: W"); // This sentence just isn't base32 encoded. assertFailsToDecode(base32Hex(), "let's not talk of love or chains!"); // An 8n+{1,3,6} length string is never legal base32. assertFailsToDecode(base32Hex(), "A"); assertFailsToDecode(base32Hex(), "ABC"); assertFailsToDecode(base32Hex(), "ABCDEF"); }
/** * Decode an auth token. * * @param token * @param request * @return an array with three elements: the first contains the actual * {@link AccessToken} and the second contains the environment that * the token was encoded with and the third contains the fingerprint * @throws GeneralSecurityException */ public static HttpAuthToken decodeAuthToken(String token) throws GeneralSecurityException { ByteBuffer cryptPack = ByteBuffer .wrap(BaseEncoding.base64Url().decode(token)); String pack = ByteBuffers.getString(ClientSecurity.decrypt(cryptPack)); String[] toks = pack.split("\\|"); return new HttpAuthToken( new AccessToken(ByteBuffer .wrap(BaseEncoding.base32Hex().decode(toks[0]))), toks[1], toks[2]); }
@GwtIncompatible // Reader/Writer public void testBase32HexStreaming() throws IOException { // The following test vectors are specified in RFC 4648 itself testStreamingEncodingWithCasing(base32Hex(), "", ""); testStreamingEncodingWithCasing(base32Hex(), "f", "CO======"); testStreamingEncodingWithCasing(base32Hex(), "fo", "CPNG===="); testStreamingEncodingWithCasing(base32Hex(), "foo", "CPNMU==="); testStreamingEncodingWithCasing(base32Hex(), "foob", "CPNMUOG="); testStreamingEncodingWithCasing(base32Hex(), "fooba", "CPNMUOJ1"); testStreamingEncodingWithCasing(base32Hex(), "foobar", "CPNMUOJ1E8======"); }
public void testToString() { assertEquals("BaseEncoding.base64().withPadChar('=')", base64().toString()); assertEquals("BaseEncoding.base32Hex().omitPadding()", base32Hex().omitPadding().toString()); assertEquals( "BaseEncoding.base32().lowerCase().withPadChar('$')", base32().lowerCase().withPadChar('$').toString()); assertEquals( "BaseEncoding.base16().withSeparator(\"\n\", 10)", base16().withSeparator("\n", 10).toString()); } }