### Compute SHA-1 and SHA-256 Hashes Source: https://www.cryptacular.org/reference Demonstrates computing SHA-1 and SHA-256 hashes of strings and input streams using HashUtil and CodecUtil for hex encoding. Note that HashUtil supports CharSequence, InputStream, and Resource inputs directly. ```java // Compute the SHA-1 hash of a string byte[] sha1Bytes = HashUtil.sha1("Codito ergo sum"); // Compute the hex-encoded SHA-256 hash of a string String sha256Hex = CodecUtil.hex(HashUtil.sha256("Ecce homo")); // Note that HashUtil handles several input types directly: // - CharSequence (converts to UTF-8 bytes) // - InputStream (reads stream as bytes) // - Resource (reads contents as bytes) // Thus the usage for hashing a stream is the same as a string byte[] sha256Bytes = HashUtil.sha256(new FileInputStream("/path/to/data")); ``` -------------------------------- ### Password Authentication with Salted SHA-1 Hash Source: https://www.cryptacular.org/reference Illustrates a common password authentication routine using salted SHA-1 hashes, similar to how LDAP directories store password hashes. This involves creating a SaltedHash object for comparison and using HashUtil.compareHash for verification. ```java // Compute a salted SHA-1 hash of an input string and compare it to a value of // record stored as a base64-encoded string stored in "authoritativeHashString". // The stored value is thus base64(A+B), where A is the salted SHA-1 hash bytes // and B is the salt bytes. LDAP directories store password hashes this way. // Create a SaltedHash object to store the value to compare against SaltedHash authoritativeHash = new SaltedHash( CodecUtil.b64(authoritativeHashString), // String to bytes 20, // SHA-1 hashes are 160 bits/20 bytes long, true); // Salt is appended to the end of the hash bytes // Perform the hash comparison boolean areEqual = HashUtil.compareHash( new org.bouncycastle.crypto.digests.SHA1Digest(), authoritativeHash, 1, // One hashing round, true, // Salt is appended to the end of the hash bytes "Th3P@ssw0rd"); // Input string to hash and compare ``` -------------------------------- ### Encrypt and Decrypt File with AES-GCM Source: https://www.cryptacular.org/reference This snippet demonstrates a full encryption and decryption cycle for a file using AES in GCM mode. It requires setting up a BlockCipher, an AEADBlockCipher, a SecretKey, and a Nonce generator. The process involves reading from an input stream and writing to an output stream, with base64 encoding for the ciphertext. ```java BlockCipher block = org.bouncycastle.crypto.engines.AESEngine.newInstance(); AEADBlockCipher cipher = org.bouncycastle.crypto.modes.GCMBlockCipher.newInstance(block); SecretKey key = SecretKeyGenerator.generate(128, block); Nonce nonce = new org.cryptacular.generator.sp80038d.RBGNonce(); //============ // Encryption //============ InputStream inPlain = new FileInputStream("/path/to/plain.txt"); OutputStream outCipher = new EncodingOutputStream( new FileOutputStream("/path/to/cipher.b64"), new Base64Encoder(72)); CipherUtil.encrypt(cipher, key, nonce, inPlain, outCipher); //============ // Decryption //============ InputStream inCipher = new DecodingInputStream( new FileInputStream("/path/to/cipher.b64"), new Base64Decoder()); OutputStream outPlain = new FileOutputStream("/path/to/plain2.txt"); CipherUtil.decrypt(cipher, key, inCipher, outPlain); ``` -------------------------------- ### Java Service for Encryption and Decryption Source: https://www.cryptacular.org/reference Implements encryption and decryption using an injected CipherBean and Base64 encoding. The service handles conversion between strings and bytes. ```java @org.springframework.stereotype.Service public class EncryptionService() { @Inject private CipherBean cipherBean; private final Encoder encoder = new Base64Encoder(72); private final Decoder decoder = new Base64Decoder(); public String encrypt(String plainText) { return CodecUtil.encode(encoder, cipherBean.encrypt(ByteUtil.toBytes(plainText))); } public String decrypt(String cipherText) { return ByteUtil.toString(cipherBean.decrypt(CodecUtil.decode(decoder, cipherText))); } } ``` -------------------------------- ### Java Service for Password Hashing Source: https://www.cryptacular.org/reference Provides a method to hash passwords using an injected HashBean. The service relies on the HashBean to handle byte conversions for common input types. ```java @org.springframework.stereotype.Service public class PasswordHashService() { @Inject private HashBean hashBean; public String hash(String password, byte[] salt) { // Hash beans handle conversion of objects to bytes for common input types // See HashUtil#hash() JavaDocs for more information return hashBean.hash(password, salt); } } ``` -------------------------------- ### Spring XML Configuration for Encryption Beans Source: https://www.cryptacular.org/reference Configures beans for encryption and decryption using AEADBlockCipherSpec and RBGNonce. Ensure the keystore path and key alias are correctly set. ```xml ``` -------------------------------- ### Spring XML Configuration for Hashing Beans Source: https://www.cryptacular.org/reference Configures a bean for generating salted, hex-encoded SHA-256 hash digests. The number of iterations can be adjusted. ```xml ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.