/** * Load a BlobCrypter from the specified file. Override this if you have your own * BlobCrypter implementation. */ protected BlobCrypter loadCrypterFromFile(File file) throws IOException { return new BasicBlobCrypter(file); }
/** * Builds a BlobCrypter from the specified master key * * @param masterKey */ public BasicBlobCrypter(byte[] masterKey) { init(masterKey); }
@Test(expected=BlobCrypterException.class) public void testBadKey() throws Exception { BlobCrypter alt = new BasicBlobCrypter("1123456789abcdef".getBytes(),HMACType.HMACSHA1); Map<String, String> in = ImmutableMap.of("a","b"); String blob = crypter.wrap(in); alt.unwrap(blob); }
private void assertThrowsBlobCrypterException(String in) { try { crypter.unwrap(in); fail("Should have thrown BlobCrypterException for input " + in); } catch (BlobCrypterException e) { // Good. } }
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) 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); } }
@Test(expected=BlobCrypterException.class) public void testBadKey() throws Exception { BlobCrypter alt = new BasicBlobCrypter("1123456789abcdef".getBytes()); Map<String, String> in = ImmutableMap.of("a","b"); String blob = crypter.wrap(in); alt.unwrap(blob, 30); }
private void assertThrowsBlobCrypterException(String in) { try { crypter.unwrap(in, 1000); fail("Should have thrown BlobCrypterException for input " + in); } catch (BlobCrypterException e) { // Good. } }
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) 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); } }
/** * Load a BlobCrypter from the specified file. Override this if you have your own * BlobCrypter implementation. */ protected BlobCrypter loadCrypterFromFile(File file) throws IOException { return new BasicBlobCrypter(file); }
@Test(expected=BlobCrypterException.class) public void testBadKey() throws Exception { BlobCrypter alt = new BasicBlobCrypter("1123456789abcdef".getBytes()); Map<String, String> in = ImmutableMap.of("a","b"); String blob = crypter.wrap(in); alt.unwrap(blob, 30); }
/** * Builds a BlobCrypter from the specified master key * * @param masterKey */ public BasicBlobCrypter(byte[] masterKey) { init(masterKey); }
private void assertThrowsBlobCrypterException(String in) { try { crypter.unwrap(in, 1000); fail("Should have thrown BlobCrypterException for input " + in); } catch (BlobCrypterException e) { // Good. } }
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); } }