本文整理汇总了Java中java.security.MessageDigest.getDigestLength方法的典型用法代码示例。如果您正苦于以下问题:Java MessageDigest.getDigestLength方法的具体用法?Java MessageDigest.getDigestLength怎么用?Java MessageDigest.getDigestLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.MessageDigest
的用法示例。
在下文中一共展示了MessageDigest.getDigestLength方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertMessageDigestHashing
import java.security.MessageDigest; //导入方法依赖的package包/类
private static void assertMessageDigestHashing(byte[] input, String algorithmName) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithmName);
assertEquals(
HashCode.fromBytes(digest.digest(input)),
ALGORITHMS.get(algorithmName).hashBytes(input));
for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) {
assertEquals(
HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input));
}
try {
int maxSize = digest.getDigestLength();
new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName);
fail();
} catch (IllegalArgumentException expected) {
}
} catch (NoSuchAlgorithmException nsae) {
throw new AssertionError(nsae);
}
}
示例2: runTest
import java.security.MessageDigest; //导入方法依赖的package包/类
private boolean runTest(String algo, long dataLen,
UpdateMethod whichUpdate) throws Exception {
try {
// Do initialization
byte[] data = new byte[(int) dataLen];
new Random().nextBytes(data);
MessageDigest md = MessageDigest.getInstance(algo);
int outputLen = md.getDigestLength();
// Perform the update using all available/possible update methods
whichUpdate.updateDigest(data, md, dataLen);
// Get the output
byte[] output = md.digest();
// Compare input and output
return outputLen == output.length;
} catch (Exception ex) {
System.err.println("Testing: " + algo + "/" + dataLen + "/"
+ whichUpdate.toString()
+ " failed with unexpected exception");
ex.printStackTrace();
throw ex;
}
}
示例3: Hash
import java.security.MessageDigest; //导入方法依赖的package包/类
public Hash(String digestAlgorithm, String digestProvider, SecurityParameters securityParameters, Conversion conversion) {
this.digestAlgorithm = digestAlgorithm;
this.digestProvider = digestProvider;
this.conversion = conversion;
this.securityParameters = securityParameters;
MessageDigest messageDigest = newMessageDigest();
if (messageDigest.getDigestLength() < securityParameters.getUpper_l()) {
throw new IllegalArgumentException(
String.format(
"The length of the message digest should be greater or equal to the expected output " +
"length. Got %d expected %d (bytes)",
messageDigest.getDigestLength(),
securityParameters.getUpper_l()));
}
}
示例4: TreeChunker
import java.security.MessageDigest; //导入方法依赖的package包/类
public TreeChunker(int branches, MessageDigest hasher) {
this.branches = branches;
this.hasher = hasher;
hashSize = hasher.getDigestLength();
chunkSize = hashSize * branches;
}
示例5: SimpleNonceManager
import java.security.MessageDigest; //导入方法依赖的package包/类
public SimpleNonceManager(final String hashAlg) {
// Verify it is a valid algorithm (at least for now)
MessageDigest digest = getDigest(hashAlg);
this.hashAlg = hashAlg;
this.hashLength = digest.getDigestLength();
// Create a new secret only valid within this NonceManager instance.
Random rand = new SecureRandom();
byte[] secretBytes = new byte[32];
rand.nextBytes(secretBytes);
secret = FlexBase64.encodeString(digest.digest(secretBytes), false);
}
示例6: fullDomainHash
import java.security.MessageDigest; //导入方法依赖的package包/类
/**
* Calculates a full domain hash (FDH) for the given byte array and the given maximum, where
* <code>FDH < max</code>
*
* @param max the maximum value of the resulting FDH
* @param m message for which the hash is calculated
* @return a big integer representing the full domain hash
* @throws NoSuchAlgorithmException if no SHA-512 implementation is found
*/
public static BigInteger fullDomainHash(BigInteger max, byte[] m) throws NoSuchAlgorithmException {
BigInteger fdh;
BigInteger counter = BigInteger.ZERO;
MessageDigest md = MessageDigest.getInstance("SHA-512");
int digestLength = md.getDigestLength();
int bitLength = max.bitLength();
byte[] shortHash = getShortHash(md, m, counter);
byte[] largeHash = shortHash;
counter = counter.add(BigInteger.ONE);
// concatenate new small hashes to the large hash until the bitlenght of the large hash is bigger than or equal
// to the bitlength of max
while ((largeHash.length * 8) < bitLength) {
shortHash = getShortHash(md, m, counter);
counter = counter.add(BigInteger.ONE);
largeHash = new ByteArray(largeHash).concat(shortHash).getArray();
}
fdh = convertHashToBigInt(largeHash, bitLength);
// while resulting fdh is too big, cut off the first part of the fdh and append a new short hash
while (fdh.compareTo(max) > 0) {
counter = counter.add(BigInteger.ONE);
shortHash = getShortHash(md, m, counter);
System.arraycopy(largeHash, digestLength, largeHash, 0, largeHash.length - digestLength);
System.arraycopy(shortHash, 0, largeHash, largeHash.length - digestLength, digestLength);
fdh = convertHashToBigInt(largeHash, bitLength);
}
return fdh;
}
示例7: updateDigest
import java.security.MessageDigest; //导入方法依赖的package包/类
@Override
public byte[] updateDigest(byte[] data, MessageDigest md)
throws DigestException {
for (byte element: data) {
md.update(element);
}
byte[] output = new byte[md.getDigestLength()];
int len = md.digest(output, 0, output.length);
if (len != output.length) {
throw new RuntimeException(
"ERROR" + ": digest length differs!");
}
return output;
}
示例8: updateDigest
import java.security.MessageDigest; //导入方法依赖的package包/类
@Override
public byte[] updateDigest(byte[] data, MessageDigest md)
throws DigestException {
for (byte element : data) {
md.update(element);
}
byte[] output = new byte[md.getDigestLength()];
int len = md.digest(output, 0, output.length);
if (len != output.length) {
throw new RuntimeException(
"ERROR" + ": digest length differs!");
}
return output;
}
示例9: runTest
import java.security.MessageDigest; //导入方法依赖的package包/类
private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate)
throws Exception {
try {
// Do initialization
byte[] data = new byte[(int) dataLen];
RandomFactory.getRandom().nextBytes(data);
MessageDigest md = MessageDigest.getInstance(algo);
int outputLen = md.getDigestLength();
// Perform the update using all available/possible update methods
whichUpdate.updateDigest(data, md, dataLen);
// Get the output
byte[] output = md.digest();
// Compare input and output
return outputLen == output.length;
} catch (NoSuchAlgorithmException nae) {
if (algo.startsWith("SHA3") && !isSHA3supported()) {
return true;
}
throw nae;
} catch (Exception ex) {
System.err.println("Testing: " + algo + "/" + dataLen + "/"
+ whichUpdate.toString()
+ " failed with unexpected exception");
ex.printStackTrace();
throw ex;
}
}
示例10: concatKDF
import java.security.MessageDigest; //导入方法依赖的package包/类
/**
* Implementation of Concatenation Key Derivation Function
* http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf
*
* Author: NimbusDS Lai Xin Chu and Vladimir Dzhuvinov
*
* See https://code.google.com/p/openinfocard/source/browse/trunk/testsrc/org/xmldap/crypto/ConcatKeyDerivationFunction.java?r=770
*/
private static byte[] concatKDF(MessageDigest md, byte[] z, int keyDataLen, byte[] otherInfo) {
final long MAX_HASH_INPUTLEN = Long.MAX_VALUE;
final long UNSIGNED_INT_MAX_VALUE = 4294967295L;
keyDataLen = keyDataLen/8;
byte[] key = new byte[keyDataLen];
int hashLen = md.getDigestLength();
int reps = keyDataLen / hashLen;
if (reps > UNSIGNED_INT_MAX_VALUE) {
getLogger().error("Key derivation failed");
return null;
}
int counter = 1;
byte[] counterInBytes = ByteUtils.intToFourBytes(counter);
if ((counterInBytes.length + z.length + otherInfo.length) * 8 > MAX_HASH_INPUTLEN) {
getLogger().error("Key derivation failed");
return null;
}
for (int i = 0; i <= reps; i++) {
md.reset();
md.update(ByteUtils.intToFourBytes(i+1));
md.update(z);
md.update(otherInfo);
byte[] hash = md.digest();
if (i < reps) {
System.arraycopy(hash, 0, key, hashLen * i, hashLen);
} else {
if (keyDataLen % hashLen == 0) {
System.arraycopy(hash, 0, key, hashLen * i, hashLen);
} else {
System.arraycopy(hash, 0, key, hashLen * i, keyDataLen % hashLen);
}
}
}
return key;
}