protected String hmacSignAndEncode(String data, String key) throws UnsupportedEncodingException { MacSigner macSigner = new MacSigner(key); return new String(Base64.encodeBase64URLSafe(macSigner.sign(data.getBytes("UTF-8"))), "UTF-8"); }
private String getLogFileBasename() { ArrayList<String> basenameElements = new ArrayList<String>(); basenameElements.add(Integer.toString(mGeneration)); if (mKafkaPartitions.length > 1) { String kafkaPartitions = mKafkaPartitions[0] + "-" + mKafkaPartitions[mKafkaPartitions.length - 1]; basenameElements.add(kafkaPartitions); StringBuilder sb = new StringBuilder(); for (long offset : mOffsets) { sb.append(offset); } try { byte[] md5Bytes = messageDigest.digest(sb.toString().getBytes("UTF-8")); byte[] encodedBytes = Base64.encodeBase64URLSafe(md5Bytes); basenameElements.add(new String(encodedBytes)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else { basenameElements.add(Integer.toString(mKafkaPartitions[0])); basenameElements.add(String.format("%020d", mOffsets[0])); } return StringUtils.join(basenameElements, "_"); }
/** * Returns an encoded and decoded copy of the same random data. * * @param size amount of random data to generate and encode * @param urlSafe true if encoding be urlSafe * @return two byte[] arrays: [0] = decoded, [1] = encoded */ static byte[][] randomData(final int size, final boolean urlSafe) { final Random r = new Random(); final byte[] decoded = new byte[size]; r.nextBytes(decoded); final byte[] encoded = urlSafe ? Base64.encodeBase64URLSafe(decoded) : Base64.encodeBase64(decoded); return new byte[][] {decoded, encoded}; }
@Test public void verify_hmac_256_signature() throws Exception { String key = "key"; String data = "data"; SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); byte[] hmacData = mac.doFinal(data.getBytes("UTF-8")); assertThat(new String(Base64.encodeBase64URLSafe(hmacData)), equalTo(xoAuthAuthenticationManager.hmacSignAndEncode(data, key))); }
final byte[] encodedUrlSafe = Base64.encodeBase64URLSafe(ids[i]); final byte[] decodedStandard = Base64.decodeBase64(standard[i]); final byte[] decodedUrlSafe1 = Base64.decodeBase64(urlSafe1[i]);
/** * Encodes a byte array to Base64. URL safe. * @param str the byte array * @return an encoded string */ public static String base64encURL(byte[] str) { if (str == null) { return ""; } return new String(Base64.encodeBase64URLSafe(str)); }
/** * Encodes a byte array to Base64. URL safe. * @param str the byte array * @return an encoded string */ public static String base64encURL(byte[] str) { if (str == null) { return ""; } return new String(Base64.encodeBase64URLSafe(str)); }
/** * Encode a byte[] into Base 64 encoding * * @param value * The byte[] to encode into base 64 encoding * @return The {@link String} encoded into base 64. */ public static String base64Encode(byte[] value) { return new String(Base64.encodeBase64URLSafe(value), StandardCharsets.UTF_8); }
/** * Gets the base64 data. * * @return the base64 data */ public final String getBase64DataUrlSafe() { return new String(Base64.encodeBase64URLSafe(bytes), Charset.forName("UTF-8")); }
/** * Utility function to encrypt a plainText with a baseKey * * @author Nidhish * @param plainText - valid plain text * @param baseKey - valid base key * @return Encrypted string value * @throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException */ public static String encrypt(String plainText, final String baseKey) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { SecretKey secretKey = getSecretKey(baseKey); byte[] plainTextByte = plainText.getBytes(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, getIvParameterSpec()); byte[] encryptedByte = cipher.doFinal(plainTextByte); return new String(Base64.encodeBase64URLSafe(encryptedByte)); }
/** * Generate an opaque pagination token from the supplied PageMetadata. * * @param pageMetadata page metadata of the page for which the token should be generated * @return opaque pagination token */ static String tokenize(PageMetadata<?, ?> pageMetadata) { try { Gson g = getGsonWithKeyAdapter(pageMetadata.pageRequestParameters); return new String(Base64.encodeBase64URLSafe(g.toJson(new PaginationToken (pageMetadata)).getBytes("UTF-8")), Charset.forName("UTF-8")); } catch (UnsupportedEncodingException e) { //all JVMs should support UTF-8 throw new RuntimeException(e); } }
/** * This returns the base64url encoding of the left-most half of the hash of the octets of the ASCII representation * of the param value. * The hash algorithm used is the hash algorithm used in the alg Header Parameter of the ID Token's JOSE Header. * This method generate both c_hash and at_hash values when value is given as authorization code and access token * respectively. * @param value * @return at_hash or c_hash value * @throws IdentityOAuth2Exception */ private String getHashValue(String value) throws IdentityOAuth2Exception { String digAlg = OAuth2Util.mapDigestAlgorithm(signatureAlgorithm); MessageDigest md; try { md = MessageDigest.getInstance(digAlg); } catch (NoSuchAlgorithmException e) { throw new IdentityOAuth2Exception("Error creating the hash value. Invalid Digest Algorithm: " + digAlg); } md.update(value.getBytes(Charsets.UTF_8)); byte[] digest = md.digest(); int leftHalfBytes = 16; if (SHA384.equals(digAlg)) { leftHalfBytes = 24; } else if (SHA512.equals(digAlg)) { leftHalfBytes = 32; } byte[] leftmost = new byte[leftHalfBytes]; System.arraycopy(digest, 0, leftmost, 0, leftHalfBytes); return new String(Base64.encodeBase64URLSafe(leftmost), Charsets.UTF_8); }
processConfig.setComment(new String(Base64.encodeBase64URLSafe(bytes))); if (msg != null) { byte[] bytes = msg.getBytes(MESSAGES_CHARSET); msg = new String(Base64.encodeBase64URLSafe(bytes), MESSAGES_CHARSET); processConfig.getMessages().put(langCode, msg); processConfig.setDictionary(new String(Base64.encodeBase64URLSafe(dictionaryInBytes)));
public String wrap(Map<String, String> in) throws BlobCrypterException { Preconditions.checkArgument(!in.containsKey(TIMESTAMP_KEY), "No '%s' key allowed for BlobCrypter", TIMESTAMP_KEY); try { byte[] encoded = serializeAndTimestamp(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha1(hmacKey, cipherText); byte[] b64 = Base64.encodeBase64URLSafe(Crypto.concat(cipherText, hmac)); return new String(b64, UTF8); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(e); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
_value = new String(Base64.encodeBase64URLSafe(DESCodec.encrypt(value.getBytes(getCharsetEncoding()), __cookieKey.getBytes())), getCharsetEncoding()); } catch (Exception e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); _value = new String(Base64.encodeBase64URLSafe(_value.getBytes(getCharsetEncoding())), getCharsetEncoding()); } catch (UnsupportedEncodingException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e));
public String wrap(Map<String, String> in) throws BlobCrypterException { Preconditions.checkArgument(!in.containsKey(TIMESTAMP_KEY), "No '%s' key allowed for BlobCrypter", TIMESTAMP_KEY); try { byte[] encoded = serializeAndTimestamp(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha1(hmacKey, cipherText); byte[] b64 = Base64.encodeBase64URLSafe(Crypto.concat(cipherText, hmac)); return new String(b64, UTF8); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(e); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
String referencePKCECodeChallenge = new String(Base64.encodeBase64URLSafe(hash), StandardCharsets.UTF_8).trim(); if (!referencePKCECodeChallenge.equals(referenceCodeChallenge)) {
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }