/** * @param secret * @return */ public String decrypt(String secret) { try { Cipher cipher = Cryptos.getAES(Cipher.ENCRYPT_MODE, keyHash, settings.cryptoKeySize()); return Codecs.toUTF8(cipher.doFinal(Codecs.decodeBase64(secret, false))); } catch (BadPaddingException | IllegalBlockSizeException ex) { throw AppeException.wrap(ex); } }
/** * Generate 32 bytes secret * * @return */ public String randomSecret() { return Codecs.encodeBase64(PRNG.nextBytes(32), false); }
/** * return a hash of a given secret if any. * * @param secret * @return */ public String hash(String secret) { return Codecs.encodeBase64( Digests.sha2(settings.cryptoHashSalt(), Codecs.toBytes(secret)), false); }
/** @param settings */ public CredentialsCrypto(AccountSettings settings) { this.settings = settings; this.keyHash = Digests.sha2(Codecs.toBytes(settings.cryptoKeySeed())); }