Exemplo n.º 1
0
    private DigestRandomGenerator(MessageDigest digest) {
      this.digest = digest;

      this.seed = new byte[digest.getDigestLength()];
      this.seedCounter = 1;

      this.state = new byte[digest.getDigestLength()];
      this.stateCounter = 1;
    }
Exemplo n.º 2
0
 /**
  * Computes a hash code based on the data that have been provided to this hasher. The result is
  * unspecified if this method is called more than once on the same instance.
  */
 BytesHashCode hash() {
   checkNotDone();
   done = true;
   return (bytes == digest.getDigestLength())
       ? new BytesHashCode(digest.digest())
       : new BytesHashCode(Arrays.copyOf(digest.digest(), bytes));
 }
 @Override
 public HashCode hash() {
   done = true;
   return (bytes == digest.getDigestLength())
       ? HashCode.fromBytesNoCopy(digest.digest())
       : HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes));
 }
Exemplo n.º 4
0
  /**
   * Generate the mask.
   *
   * @param seed source of input bytes for initial digest state
   * @param length length of mask to generate
   * @return a byte array containing a MGF1 generated mask
   */
  public byte[] generateMask(byte[] seed, int length) {
    byte[] mask = new byte[length];
    byte[] C = new byte[4];
    int counter = 0;
    int hLen = digest.getDigestLength();

    digest.reset();

    while (counter < (length / hLen)) {
      ItoOSP(counter, C);

      digest.update(seed);
      digest.update(C);

      System.arraycopy(digest.digest(), 0, mask, counter * hLen, hLen);

      counter++;
    }

    if ((counter * hLen) < length) {
      ItoOSP(counter, C);

      digest.update(seed);
      digest.update(C);

      System.arraycopy(digest.digest(), 0, mask, counter * hLen, mask.length - (counter * hLen));
    }

    return mask;
  }
Exemplo n.º 5
0
 private SHA256HashFunction(int bytes) {
   int maxLength = digest.getDigestLength();
   if (!(bytes >= 4 && bytes <= maxLength))
     throw new IllegalArgumentException(
         String.format("bytes (%d) must be >= 4 and < %d.", bytes, maxLength));
   this.bytes = bytes;
   this.supportsClone = supportsClone();
 }
 MessageDigestHashFunction(String algorithmName, int bytes, String toString) {
   this.toString = checkNotNull(toString);
   this.prototype = getMessageDigest(algorithmName);
   int maxLength = prototype.getDigestLength();
   checkArgument(
       bytes >= 4 && bytes <= maxLength, "bytes (%s) must be >= 4 and < %s", bytes, maxLength);
   this.bytes = bytes;
   this.supportsClone = supportsClone();
 }
 static {
   try {
     DIGESTER = MessageDigest.getInstance(DIGEST_MD5);
     FINGERPRINT_BYTE_LENGTH = DIGESTER.getDigestLength();
     STREAM_ID_CS_01_LENGTH = STREAM_ID_CS_PREFIX.length() + (FINGERPRINT_BYTE_LENGTH * 2);
   } catch (NoSuchAlgorithmException e) {
     // can't continue, but really shouldn't happen
     throw new IllegalStateException(e);
   }
 }
 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);
   }
 }
Exemplo n.º 9
0
 /** Construct a new RSASignature. Used by subclasses. */
 RSASignature(String algorithm, int oidLength) {
   // this.digestOID = digestOID;
   try {
     md = MessageDigest.getInstance(algorithm);
     // AlgorithmId x
   } catch (NoSuchAlgorithmException e) {
     throw new ProviderException(e);
   }
   digestReset = true;
   encodedLength = baseLength + oidLength + md.getDigestLength();
 }
Exemplo n.º 10
0
  public static String MD5(String source) throws Exception {
    String resultHash = null;
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    byte[] result = new byte[md5.getDigestLength()];
    md5.reset();
    md5.update(source.getBytes("UTF-8"));
    result = md5.digest();

    StringBuffer buf = new StringBuffer(result.length * 2);

    for (int i = 0; i < result.length; i++) {
      int intVal = result[i] & 0xff;
      if (intVal < 0x10) {
        buf.append("0");
      }
      buf.append(Integer.toHexString(intVal));
    }

    resultHash = buf.toString();

    return resultHash.toString();
  }
Exemplo n.º 11
0
 int getDigestLength() {
   return digest.getDigestLength();
 }
Exemplo n.º 12
0
 SHA256HashFunction() {
   this.bytes = digest.getDigestLength();
   this.supportsClone = supportsClone();
 }
 /** Returns the length of the digest. */
 public int digestLength() {
   return digest.getDigestLength();
 }
Exemplo n.º 14
0
 MessageDigestHashFunction(String algorithmName, String toString) {
   this.prototype = getMessageDigest(algorithmName);
   this.bytes = prototype.getDigestLength();
   this.toString = checkNotNull(toString);
   this.supportsClone = supportsClone();
 }
Exemplo n.º 15
0
  // Uses supplied hash algorithm
  static byte[] derive(
      char[] chars, byte[] salt, int ic, int n, int type, String hashAlgo, int blockLength) {

    // Add in trailing NULL terminator.  Special case:
    // no terminator if password is "\0".
    int length = chars.length * 2;
    if (length == 2 && chars[0] == 0) {
      chars = new char[0];
      length = 0;
    } else {
      length += 2;
    }

    byte[] passwd = new byte[length];
    for (int i = 0, j = 0; i < chars.length; i++, j += 2) {
      passwd[j] = (byte) ((chars[i] >>> 8) & 0xFF);
      passwd[j + 1] = (byte) (chars[i] & 0xFF);
    }
    byte[] key = new byte[n];

    try {
      MessageDigest sha = MessageDigest.getInstance(hashAlgo);

      int v = blockLength;
      int u = sha.getDigestLength();
      int c = roundup(n, u) / u;
      byte[] D = new byte[v];
      int s = roundup(salt.length, v);
      int p = roundup(passwd.length, v);
      byte[] I = new byte[s + p];

      Arrays.fill(D, (byte) type);
      concat(salt, I, 0, s);
      concat(passwd, I, s, p);

      byte[] Ai;
      byte[] B = new byte[v];
      byte[] tmp = new byte[v];

      int i = 0;
      for (; ; i++, n -= u) {
        sha.update(D);
        sha.update(I);
        Ai = sha.digest();
        for (int r = 1; r < ic; r++) Ai = sha.digest(Ai);
        System.arraycopy(Ai, 0, key, u * i, Math.min(n, u));
        if (i + 1 == c) break;
        concat(Ai, B, 0, B.length);
        BigInteger B1;
        B1 = new BigInteger(1, B).add(BigInteger.ONE);

        for (int j = 0; j < I.length; j += v) {
          BigInteger Ij;
          int trunc;

          if (tmp.length != v) tmp = new byte[v];
          System.arraycopy(I, j, tmp, 0, v);
          Ij = new BigInteger(1, tmp);
          Ij = Ij.add(B1);
          tmp = Ij.toByteArray();
          trunc = tmp.length - v;
          if (trunc >= 0) {
            System.arraycopy(tmp, trunc, I, j, v);
          } else if (trunc < 0) {
            Arrays.fill(I, j, j + (-trunc), (byte) 0);
            System.arraycopy(tmp, 0, I, j + (-trunc), tmp.length);
          }
        }
      }
    } catch (Exception e) {
      throw new RuntimeException("internal error: " + e);
    }
    return key;
  }
Exemplo n.º 16
0
 public int getDigestLength() {
   return md_.getDigestLength();
 }
Exemplo n.º 17
0
 /**
  * Returns the length of the Mac in bytes.
  *
  * @return the Mac length in bytes.
  */
 int getDigestLength() {
   return md.getDigestLength();
 }