Java 8 MessageDigest - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 MessageDigest class.

This code example shows how to use the following methods:digest, update
		if(K.length == B) // Step 1
			K0 = K;
		else { 
			// Step 2 
			if(K.length > B)
				K0 = K = Util.hashBytes(d, K);
 
			if(K.length < B) { // Step 3
				K0 = new byte[B];
				System.arraycopy(K, 0, K0, 0, K.length);
			} 
		} 
 
		// Step 4 
		byte[] IS1 = Util.xor(K0, ipad);
 
		// Step 5/6 
		d.update(IS1);
		d.update(text);
		IS1 = d.digest();

7
CodeRank
This code example shows how to use the following methods:digest, reset, update
		} 
		return getSha1(networkUid.getBytes());
	} 
 
	public static byte[] getSha1(byte[] bytes) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		md.update(SHA1_SEED);
		md.update(bytes, 0, bytes.length);
		byte[] sha1hash = md.digest();
		md.reset();
		return sha1hash;
	} 
 
	public static List<byte[]> getListSha1(String mergedSha1s) {
		byte[] merged = Base64.decode(mergedSha1s);
		if (merged.length % 20 != 0) {
			throw new RuntimeException("error, only 20 byte sha1 allowed: " + merged.length + " mod 20!=0");
		} 
		List<byte[]> listOfSha1s = new LinkedList<byte[]>();
		for (int i = 0; i < merged.length / 20; i++) {
			byte[] sha1 = new byte[20];
7
CodeRank
This code example shows how to use the following methods:digest, getAlgorithm, getDigestLength, reset, update
        if (md == null) {
            throw new NullPointerException("MessageDigest is null");
        } 
         
        // Make sure the digest length is defined 
        int digestLength = md.getDigestLength();
        if (digestLength == 0) {
            throw new IllegalArgumentException("Digest length is undefined");
        } 
         
        // Block size must be greater than zero 
        if (blockSize <= 0) {
            throw new IllegalArgumentException("Illegal block size: " + blockSize);
        } 
         
        // Block size must be greater than digest length 
        if (blockSize <= digestLength) {
            throw new IllegalArgumentException("Block size must be greater than digest length: " 
                    + blockSize + " <= " + digestLength);
        } 

7
CodeRank
This code example shows how to use the following methods:digest
  public synchronized byte[] digest(final String iInput) {
    if (iInput == null)
      return null; 
 
    try { 
      return md.digest(iInput.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      OLogManager.instance().error(this, "The requested encoding is not supported: cannot execute security checks", e,
          OConfigurationException.class); 
    } 
    return null; 
  } 
 
  public synchronized byte[] digest(byte[] iInput) {
    return md.digest(iInput);
  } 
 
  public SecretKey generateKey(final String iAlgorithm, final int iKeySize) {
    KeyGenerator kg;
    try { 
6
CodeRank
This code example shows how to use the following methods:digest, reset, update
 * Signs the data (computes the secure hash) 
 * @return An array with the signature 
 */ 
public byte [] 
sign() { 
	byte [] output = digest.digest();
	digest.reset();
	digest.update(opad);
	return digest.digest(output);
} 
 
/** 
 * Verifies the data (computes the secure hash and compares it to the input) 
 * @param signature The signature to compare against 
 * @return true if the signature matched, false otherwise 
 */ 
public boolean 
verify(byte [] signature) {
	return Arrays.equals(signature, sign());
} 
6
CodeRank
This code example shows how to use the following methods:update
            byteBuff.flip();
 
            final byte[] byts = byteBuff.array();
            final int len = byteBuff.remaining();
            final int strt = byteBuff.arrayOffset();
            digest.update(byts, strt, len);
 
        } catch (final CharacterCodingException e) {
            throw new OXFException(e);
        } catch (java.nio.BufferOverflowException e) {
            throw new OXFException(e);
        } catch (java.nio.BufferUnderflowException e) {
            throw new OXFException(e);
        } finally { 
            // Make ready for write 
            charBuff.clear();
            byteBuff.clear();
        } 
    } 
 

6
CodeRank
This code example shows how to use the following methods:digest, reset
    digest.update(out.getBytes());
  } 
 
  @Override 
  public void processFinal() { 
    printDirect(new String(Base64.encodeBase64(digest.digest())));
    digest.reset();
  } 
} 
 
6
CodeRank
This code example shows how to use the following methods:digest, reset, update
 
    // bit twiddling solution from the jglobus Attic gets it right for CAs 
    // http://www.cogkit.org/viewcvs/viewcvs.cgi/src/jglobus/src/org/globus/security/Attic/HashUtil.java?rev=HEAD&content-type=text/vnd.viewcvs-markup 
    private String hash(byte [] data) {
 
        this.md5.reset();
        this.md5.update(data);
 
        final byte[] md = md5.digest();
 
        final long ret = (fixByte(md[0]) | fixByte(md[1]) << 8L |
             fixByte(md[2])<<16L | fixByte(md[3])<<24L )&0xffffffffL;
 
        return Long.toHexString(ret);
    } 
 
    private static long fixByte(byte b) {
        return (b<0) ? (long)(b+256) : (long)b;
    } 
} 
6
CodeRank
This code example shows how to use the following methods:digest, reset
      throw new IllegalArgumentException("This should never happen");
    } 
  } 
 
  public int hash(Object s) { 
    digest.reset();
    byte[] hash = digest.digest(s.toString().getBytes());
 
    // HACK just take the first 4 digits and make it an integer. 
    // apparently this is what other algorithms use to turn it into an int 
    // value. 
 
    // http://github.com/dustin/java-memcached-client/blob/9b2b4be73ee4a74bf6d0cf47f89c33753a5b5329/src/main/java/net/spy/memcached/HashAlgorithm.java 
    int h0 = (hash[0] & 0xFF);
    int h1 = (hash[1] & 0xFF) << 8;
    int h2 = (hash[2] & 0xFF) << 16;
    int h3 = (hash[3] & 0xFF) << 24;
 
    int val = h0 + h1 + h2 + h3;
    return val;
6
CodeRank
This code example shows how to use the following methods:digest, update
	public static byte[] hashBytes( 
		MessageDigest d,
		byte[] b,
		int offset,
		int length) {
		d.update(b, offset, length);
		return d.digest();
	} 
 
	/** 
	 * Hashes a string in a consistent manner 
	 */ 
	public static byte[] hashString(MessageDigest d, String s) {
		try { 
			byte[] sbytes = s.getBytes("UTF-8");
			d.update(sbytes, 0, sbytes.length);
			return d.digest();
		} catch (Exception e) {
			e.printStackTrace();
		} 
6
CodeRank

Related Java 8 MessageDigest Questions & Answers:

See Code Examples for Java 8 MessageDigest Methods: