public Map<String, String> unwrap(String in, int maxAgeSec) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(in.getBytes("UTF-8")); byte[] hmac = new byte[Crypto.HMAC_SHA1_LEN]; byte[] cipherText = new byte[bin.length-Crypto.HMAC_SHA1_LEN]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacSha1Verify(hmacKey, cipherText, hmac); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); checkTimestamp(out, maxAgeSec); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(e); } }
public Map<String, String> unwrap(String in, int maxAgeSec) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(in.getBytes("UTF-8")); byte[] hmac = new byte[Crypto.HMAC_SHA1_LEN]; byte[] cipherText = new byte[bin.length-Crypto.HMAC_SHA1_LEN]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacSha1Verify(hmacKey, cipherText, hmac); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); checkTimestamp(out, maxAgeSec); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(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); } }
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); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", 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); } }
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); } }