当前位置: 首页>>代码示例>>Java>>正文


Java MessageDigest.getDigestLength方法代码示例

本文整理汇总了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);
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:22,代码来源:MessageDigestHashFunctionTest.java

示例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;
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:25,代码来源:TestSameLength.java

示例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()));
    }
}
 
开发者ID:republique-et-canton-de-geneve,项目名称:chvote-protocol-poc,代码行数:17,代码来源:Hash.java

示例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;
}
 
开发者ID:talentchain,项目名称:talchain,代码行数:8,代码来源:TreeChunker.java

示例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);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:14,代码来源:SimpleNonceManager.java

示例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;
}
 
开发者ID:woefe,项目名称:xmlrss,代码行数:43,代码来源:CryptoUtils.java

示例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;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:15,代码来源:TestSameValue.java

示例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;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:TestSameValue.java

示例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;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:TestSameLength.java

示例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;
   }
 
开发者ID:V2GClarity,项目名称:RISE-V2G,代码行数:51,代码来源:SecurityUtils.java


注:本文中的java.security.MessageDigest.getDigestLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。