use of java.security.DigestException in project buck by facebook.
the class DexFile method calcSignature.
/**
* Calculates the signature for the {@code .dex} file in the
* given array, and modify the array to contain it.
*
* @param bytes {@code non-null;} the bytes of the file
*/
private static void calcSignature(byte[] bytes) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
md.update(bytes, 32, bytes.length - 32);
try {
int amt = md.digest(bytes, 12, 20);
if (amt != 20) {
throw new RuntimeException("unexpected digest write: " + amt + " bytes");
}
} catch (DigestException ex) {
throw new RuntimeException(ex);
}
}
use of java.security.DigestException in project poi by apache.
the class CryptoFunctions method hashPassword.
/**
* Generalized method for read and write protection hash generation.
* The difference is, read protection uses the order iterator then hash in the hash loop, whereas write protection
* uses first the last hash value and then the current iterator value
*
* @param password
* @param hashAlgorithm
* @param salt
* @param spinCount
* @param iteratorFirst if true, the iterator is hashed before the n-1 hash value,
* if false the n-1 hash value is applied first
* @return the hashed password
*/
public static byte[] hashPassword(String password, HashAlgorithm hashAlgorithm, byte[] salt, int spinCount, boolean iteratorFirst) {
// If no password was given, use the default
if (password == null) {
password = Decryptor.DEFAULT_PASSWORD;
}
MessageDigest hashAlg = getMessageDigest(hashAlgorithm);
hashAlg.update(salt);
byte[] hash = hashAlg.digest(StringUtil.getToUnicodeLE(password));
byte[] iterator = new byte[LittleEndianConsts.INT_SIZE];
byte[] first = (iteratorFirst ? iterator : hash);
byte[] second = (iteratorFirst ? hash : iterator);
try {
for (int i = 0; i < spinCount; i++) {
LittleEndian.putInt(iterator, 0, i);
hashAlg.reset();
hashAlg.update(first);
hashAlg.update(second);
// don't create hash buffer everytime new
hashAlg.digest(hash, 0, hash.length);
}
} catch (DigestException e) {
throw new EncryptedDocumentException("error in password hashing");
}
return hash;
}
use of java.security.DigestException in project hbase by apache.
the class Encryption method hash256.
/**
* Return the SHA-256 digest of the concatenation of the supplied arguments.
*/
public static byte[] hash256(byte[]... args) {
byte[] result = new byte[32];
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
for (byte[] arg : args) {
md.update(arg);
}
md.digest(result, 0, result.length);
return result;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (DigestException e) {
throw new RuntimeException(e);
}
}
use of java.security.DigestException in project hbase by apache.
the class Encryption method hash256.
/**
* Return the SHA-256 digest of the concatenation of the supplied arguments.
*/
public static byte[] hash256(String... args) {
byte[] result = new byte[32];
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
for (String arg : args) {
md.update(Bytes.toBytes(arg));
}
md.digest(result, 0, result.length);
return result;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (DigestException e) {
throw new RuntimeException(e);
}
}
use of java.security.DigestException in project j2objc by google.
the class MessageDigest1Test method testSHAProvider.
/**
* Tests SHA MessageDigest provider
*/
public void testSHAProvider() {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
byte[] bytes = new byte[] { 1, 1, 1, 1, 1 };
// testing combination with provider
try {
// offset < 0
md.update(bytes, -1, 1);
fail("No expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
}
try {
md.update(bytes, 1, -1);
fail("No expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
}
//Regression for Harmony-1148
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
try {
// offset < 0
md.digest(bytes, 0, -1);
fail("No expected DigestException");
} catch (DigestException e) {
}
try {
// len < 0
md.digest(bytes, -1, 0);
fail("No expected DigestException");
} catch (DigestException e) {
}
try {
md = MessageDigest.getInstance("UnknownDigest");
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
}
}
Aggregations