當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。