Java Code Examples for java.security.MessageDigest#getAlgorithm()

The following examples show how to use java.security.MessageDigest#getAlgorithm() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: headlong   File: Function.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param type          to denote function, receive, fallback, or constructor
 * @param signature     the function's signature e.g. "foo(int,bool)"
 * @param outputs       the signature of the tuple containing this function's return types
 * @param messageDigest the hash function with which to generate the 4-byte selector
 * @throws IllegalArgumentException if {@code signature} or {@code outputs} is malformed
 */
public Function(Type type, String signature, String outputs, MessageDigest messageDigest) {
    final int split = signature.indexOf('(');
    if (split >= 0) {
        try {
            this.inputTypes = (TupleType) TypeFactory.create(signature.substring(split), null);
        } catch (ClassCastException cce) {
            throw new IllegalArgumentException("illegal signature termination", cce); // e.g. "foo()[]"
        }
        this.type = Objects.requireNonNull(type);
        String name = Utils.regexValidate(ALL_ASCII_NO_OPEN_PAREN, OPEN_PAREN_OR_NON_ASCII, signature.substring(0, split)); // guaranteed not to contain '(' bc of split
        this.name = name.isEmpty() && (type == Type.FALLBACK || type == Type.CONSTRUCTOR) ? null : name;
        this.outputTypes = outputs != null ? TupleType.parse(outputs) : TupleType.EMPTY;
        this.stateMutability = null;
        this.hashAlgorithm = messageDigest.getAlgorithm();
        validateFunction();
        generateSelector(messageDigest);
    } else {
        throw new IllegalArgumentException("params start not found");
    }
}
 
Example 2
private static String encode(final String s, final String alg)
{
  try {
    final MessageDigest md = MessageDigest.getInstance(alg);
    md.reset();
    md.update(s.getBytes());
    final byte[] d = md.digest();

    String ret = "";

    for (int val : d) {
      final char[] hex = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      if (val < 0) {
        val = 256 + val;
      }
      final char hi = hex[val / 16];
      final char lo = hex[val % 16];
      ret = hi + "" + lo + ret;
    }
    return md.getAlgorithm() + '{' + ret + '}';
  } catch (final NoSuchAlgorithmException ex) {
    log.fatal(ex);
    return "NONE{" + s + "}";
  }
}
 
Example 3
Source Project: yGuard   File: Writer.java    License: MIT License 5 votes vote down vote up
private void addDigests( String entryName ) {

      Attributes oldEntryAttributes = this.manifest.getAttributes( entryName );
      Attributes newEntryAttributes = new Attributes( digests.length + 1 );

      if ( null != oldEntryAttributes ) {
        Set<Object> keys = oldEntryAttributes.keySet();
        for ( Object key : keys ) {
          if ( ( (Attributes.Name) key ).toString().indexOf( "Digest" ) == -1 ) {
            newEntryAttributes.put( key, oldEntryAttributes.get( key ) );
          }
        }
      }

      StringBuffer digestsList = new StringBuffer();
      for (int i = 0; i < digests.length; i++) {
        MessageDigest digest = digests[i];

        if (null != digest) {

          String digestKey = digest.getAlgorithm() + "-Digest";
          digestsList.append(digest.getAlgorithm());
          if (i < digests.length - 1){
            digestsList.append(", ");
          }

          String digestVal = Util.toBase64(digest.digest());

          newEntryAttributes.putValue(digestKey, digestVal);
        }
      }

      newEntryAttributes.putValue("Digest-Algorithms", digestsList.toString());

      this.manifest.getEntries().put( entryName, newEntryAttributes );
    }
 
Example 4
Source Project: headlong   File: Function.java    License: Apache License 2.0 5 votes vote down vote up
public Function(Type type, String name, TupleType inputTypes, TupleType outputTypes, String stateMutability, MessageDigest messageDigest) {
    this.type = Objects.requireNonNull(type);
    this.name = name != null ? Utils.regexValidate(ALL_ASCII_NO_OPEN_PAREN, OPEN_PAREN_OR_NON_ASCII, name) : null;
    this.inputTypes = Objects.requireNonNull(inputTypes);
    this.outputTypes = Objects.requireNonNull(outputTypes);
    this.stateMutability = stateMutability;
    this.hashAlgorithm = messageDigest.getAlgorithm();
    validateFunction();
    generateSelector(messageDigest);
}
 
Example 5
Source Project: edslite   File: StdLayout.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected int getMKKDFNumIterations(MessageDigest hashFunc)
{
	String an = hashFunc.getAlgorithm();
	if("ripemd160".equalsIgnoreCase(an))
		return 2000;
	return 1000;
}
 
Example 6
public static String getHashFuncName(MessageDigest md)
{
    if(md instanceof RIPEMD160)
        return "RIPEMD-160";
    if(md instanceof Whirlpool)
        return "Whirlpool";
    return md.getAlgorithm();
}
 
Example 7
private String getHashFuncName(MessageDigest hf)
{
    return hf.getAlgorithm();
}
 
Example 8
/**
 * given the .SF digest header, and the data from the
 * section in the manifest, see if the hashes match.
 * if not, throw a SecurityException.
 *
 * @return true if all the -Digest headers verified
 * @exception SecurityException if the hash was not equal
 */

private boolean verifySection(Attributes sfAttr,
                              String name,
                              ManifestDigester md)
     throws IOException, SignatureException
{
    boolean oneDigestVerified = false;
    ManifestDigester.Entry mde = md.get(name,block.isOldStyle());

    if (mde == null) {
        throw new SecurityException(
              "no manifest section for signature file entry "+name);
    }

    if (sfAttr != null) {

        //sun.misc.HexDumpEncoder hex = new sun.misc.HexDumpEncoder();
        //hex.encodeBuffer(data, System.out);

        // go through all the attributes and process *-Digest entries
        for (Map.Entry<Object,Object> se : sfAttr.entrySet()) {
            String key = se.getKey().toString();

            if (key.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) {
                // 7 is length of "-Digest"
                String algorithm = key.substring(0, key.length()-7);

                MessageDigest digest = getDigest(algorithm);

                if (digest != null) {
                    boolean ok = false;

                    byte[] expected =
                        Base64.getMimeDecoder().decode((String)se.getValue());
                    byte[] computed;
                    if (workaround) {
                        computed = mde.digestWorkaround(digest);
                    } else {
                        computed = mde.digest(digest);
                    }

                    if (debug != null) {
                      debug.println("Signature Block File: " +
                               name + " digest=" + digest.getAlgorithm());
                      debug.println("  expected " + toHex(expected));
                      debug.println("  computed " + toHex(computed));
                      debug.println();
                    }

                    if (MessageDigest.isEqual(computed, expected)) {
                        oneDigestVerified = true;
                        ok = true;
                    } else {
                        // attempt to fallback to the workaround
                        if (!workaround) {
                           computed = mde.digestWorkaround(digest);
                           if (MessageDigest.isEqual(computed, expected)) {
                               if (debug != null) {
                                   debug.println("  re-computed " + toHex(computed));
                                   debug.println();
                               }
                               workaround = true;
                               oneDigestVerified = true;
                               ok = true;
                           }
                        }
                    }
                    if (!ok){
                        throw new SecurityException("invalid " +
                                   digest.getAlgorithm() +
                                   " signature file digest for " + name);
                    }
                }
            }
        }
    }
    return oneDigestVerified;
}
 
Example 9
Source Project: stratio-cassandra   File: Validator.java    License: Apache License 2.0 4 votes vote down vote up
public CountingDigest(MessageDigest underlying)
{
    super(underlying.getAlgorithm());
    this.underlying = underlying;
}