List of usage examples for org.bouncycastle.crypto.digests GeneralDigest getDigestSize
public int getDigestSize();
From source file:com.stg.crypto.CryptoHelper.java
License:Open Source License
/** * This is a general purpose checksum generator method. * //from w ww .j a v a 2 s. co m * @param digest Any class that extends {@link GeneralDigest}. * @param bytes bytes on which the checksum is to be generated. * @return checksum as per the digest. * @see CryptoHelper#generateMD5Checksum(byte[]) * @see CryptoHelper#generateMD5Checksum(InputStream) * @see CryptoHelper#generateMD5Checksum(String) * @see CryptoHelper#generateSHA1Checksum(byte[]) * @see CryptoHelper#generateSHA1Checksum(InputStream) * @see CryptoHelper#generateSHA1Checksum(String) */ public static String generateChecksum(GeneralDigest digest, byte[] bytes) { digest.update(bytes, 0, bytes.length); byte[] checkSum = new byte[digest.getDigestSize()]; digest.doFinal(checkSum, 0); return toString(Hex.encode(checkSum)); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleTokenDecrypter.java
License:Apache License
@Override public String decrypt(String ciphertext) throws InvalidCipherTextException { final Matcher tokenMatcher = TOKEN_PATTERN.matcher(ciphertext); if (tokenMatcher.matches()) { ciphertext = tokenMatcher.group(1); } else {/*from w w w . j ava 2s. c o m*/ final Matcher base64Matcher = BASE64_PATTERN.matcher(ciphertext); if (!base64Matcher.matches()) { throw new IllegalArgumentException("Specified ciphertext is not valid"); } } //Decode the cipher text final byte[] encryptedTokenWithHash = Base64.decodeBase64(ciphertext); final AsymmetricBlockCipher e = getDecryptCipher(); final byte[] tokenWithHashBytes = e.processBlock(encryptedTokenWithHash, 0, encryptedTokenWithHash.length); //Split the decrypted text into the password and the hash final String tokenWithHash = new String(tokenWithHashBytes, BouncyCastleTokenEncrypter.CHARSET); final int seperatorIndex = tokenWithHash.lastIndexOf(BouncyCastleTokenEncrypter.SEPARATOR); if (seperatorIndex < 0) { throw new IllegalArgumentException( "token/hash string doesn't contain seperator: " + BouncyCastleTokenEncrypter.SEPARATOR); } final byte[] passwordBytes = tokenWithHash.substring(0, seperatorIndex) .getBytes(BouncyCastleTokenEncrypter.CHARSET); final byte[] passwordHashBytes = Base64.decodeBase64( tokenWithHash.substring(seperatorIndex + 1).getBytes(BouncyCastleTokenEncrypter.CHARSET)); //Generate hash of the decrypted password final GeneralDigest digest = this.createDigester(); digest.update(passwordBytes, 0, passwordBytes.length); final byte[] expectedHashBytes = new byte[digest.getDigestSize()]; digest.doFinal(expectedHashBytes, 0); //Verify the generated hash against the decrypted hash if (!Arrays.equals(expectedHashBytes, passwordHashBytes)) { throw new IllegalArgumentException("Hash of the decrypted token does not match the decrypted hash"); } return new String(passwordBytes, BouncyCastleTokenEncrypter.CHARSET); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleTokenEncrypter.java
License:Apache License
@Override public String encrypt(String token) throws InvalidCipherTextException { //Convert the token into a byte[] final byte[] tokenBytes = token.getBytes(CHARSET); //Generate the Base64 encoded hash of the token final GeneralDigest digest = createDigester(); digest.update(tokenBytes, 0, tokenBytes.length); final byte[] hashBytes = new byte[digest.getDigestSize()]; digest.doFinal(hashBytes, 0);//from w ww . j a v a 2s . c o m final byte[] encodedHashBytes = Base64.encodeBase64(hashBytes); //Create the pre-encryption byte[] to hold the token, separator, and hash final byte[] tokenWithHashBytes = new byte[tokenBytes.length + SEPARATOR_BYTES.length + encodedHashBytes.length]; //Copy in password bytes System.arraycopy(tokenBytes, 0, tokenWithHashBytes, 0, tokenBytes.length); //Copy in separator bytes System.arraycopy(SEPARATOR_BYTES, 0, tokenWithHashBytes, tokenBytes.length, SEPARATOR_BYTES.length); //Copy in encoded hash bytes System.arraycopy(encodedHashBytes, 0, tokenWithHashBytes, tokenBytes.length + SEPARATOR_BYTES.length, encodedHashBytes.length); AsymmetricBlockCipher e = getEncryptCipher(); //Encrypt the bytes final byte[] encryptedTokenWithHash = e.processBlock(tokenWithHashBytes, 0, tokenWithHashBytes.length); //Encode the encrypted data and convert it into a string final String encryptedToken = new String(Base64.encodeBase64(encryptedTokenWithHash), CHARSET); return TOKEN_PREFIX + encryptedToken + TOKEN_SUFFIX; }
From source file:org.bunkr.cli.commands.HashCommand.java
License:Open Source License
private byte[] calculateHash(ArchiveInfoContext context, FileInventoryItem target, String algorithm, boolean showProgress) throws IOException, CLIException { ProgressBar pb = new ProgressBar(120, target.getActualSize(), "Calculating hash: "); pb.setEnabled(showProgress);/* w ww.j av a 2s. c om*/ pb.startFresh(); GeneralDigest digest = getDigest(algorithm); digest.reset(); try (MultilayeredInputStream ms = new MultilayeredInputStream(context, target)) { byte[] buffer = new byte[(int) Units.MEBIBYTE]; int n; while ((n = ms.read(buffer)) != -1) { digest.update(buffer, 0, n); pb.inc(n); } Arrays.fill(buffer, (byte) 0); } pb.finish(); int length = digest.getDigestSize(); byte[] buffer = new byte[length]; digest.doFinal(buffer, 0); return buffer; }
From source file:org.bunkr.core.usersec.PasswordProvider.java
License:Open Source License
private byte[] hash(byte[] input) { GeneralDigest digest = new SHA256Digest(); digest.update(input, 0, input.length); byte[] buffer = new byte[digest.getDigestSize()]; digest.doFinal(buffer, 0);/*from w w w . ja v a2 s . c o m*/ Arrays.fill(input, (byte) 0); return buffer; }
From source file:test.bunkr.core.streams.TestBlockReaderInputStream.java
License:Open Source License
public byte[] hashUp(byte[] input) { GeneralDigest d = new SHA1Digest(); d.update(input, 0, input.length);/*from www. j av a2 s. c om*/ byte[] b = new byte[d.getDigestSize()]; d.doFinal(b, 0); return b; }