Example usage for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest

List of usage examples for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest.

Prototype

public SHA256Digest() 

Source Link

Document

Standard constructor

Usage

From source file:dorkbox.util.crypto.Crypto.java

License:Apache License

public static byte[] hashFileSHA256(File file) {
    SHA256Digest digest = new SHA256Digest();
    return hashFile(file, digest, null);
}

From source file:dorkbox.util.crypto.Crypto.java

License:Apache License

/**
 * Secure way to generate an AES key based on a password.
 *
 * @param password/*from   w  w w . j av a 2 s  .c o  m*/
 *                 The password that you want to mix
 * @param salt
 *                 should be a RANDOM number, at least 256bits (32 bytes) in size.
 * @param iterationCount
 *                 should be a lot, like 10,000
 *
 * @return the secure key to use
 */
public static byte[] PBKDF2(byte[] password, byte[] salt, int iterationCount) {
    SHA256Digest digest = new SHA256Digest();
    PBEParametersGenerator pGen = new PKCS5S2ParametersGenerator(digest);
    pGen.init(password, salt, iterationCount);

    KeyParameter key = (KeyParameter) pGen.generateDerivedMacParameters(digest.getDigestSize() * 8); // *8 for bit length.

    // zero out the password.
    Arrays.fill(password, (byte) 0);

    return key.getKey();
}

From source file:dorkbox.util.HashUtil.java

License:Apache License

/**
 * gets the SHA256 hash + SALT of the specified username, as UTF-16
 *//*from w w  w.j  av  a  2 s .  co m*/
public static byte[] getSha256WithSalt(String username, byte[] saltBytes) {
    if (username == null) {
        return null;
    }

    byte[] charToBytes = Sys.charToBytes16(username.toCharArray());
    byte[] userNameWithSalt = Sys.concatBytes(charToBytes, saltBytes);

    SHA256Digest sha256 = new SHA256Digest();
    byte[] usernameHashBytes = new byte[sha256.getDigestSize()];
    sha256.update(userNameWithSalt, 0, userNameWithSalt.length);
    sha256.doFinal(usernameHashBytes, 0);

    return usernameHashBytes;
}

From source file:dorkbox.util.HashUtil.java

License:Apache License

/**
 * gets the SHA256 hash of the specified string, as UTF-16
 *///from   w  w  w.j  a v a  2 s  .  co m
public static byte[] getSha256(String string) {
    byte[] charToBytes = Sys.charToBytes16(string.toCharArray());

    SHA256Digest sha256 = new SHA256Digest();
    byte[] usernameHashBytes = new byte[sha256.getDigestSize()];
    sha256.update(charToBytes, 0, charToBytes.length);
    sha256.doFinal(usernameHashBytes, 0);

    return usernameHashBytes;
}

From source file:dorkbox.util.HashUtil.java

License:Apache License

/**
 * gets the SHA256 hash of the specified byte array
 *//* ww  w.j a  v a2s .  c o m*/
public static byte[] getSha256(byte[] bytes) {

    SHA256Digest sha256 = new SHA256Digest();
    byte[] hashBytes = new byte[sha256.getDigestSize()];
    sha256.update(bytes, 0, bytes.length);
    sha256.doFinal(hashBytes, 0);

    return hashBytes;
}

From source file:dorkbox.util.HashUtil.java

License:Apache License

public static byte[] getSha256WithSalt(byte[] bytes, byte[] saltBytes) {
    if (bytes == null || saltBytes == null) {
        return null;
    }//from w  w w.ja  v a  2 s  .  co m

    byte[] bytesWithSalt = dorkbox.util.Sys.concatBytes(bytes, saltBytes);

    SHA256Digest sha256 = new SHA256Digest();
    byte[] usernameHashBytes = new byte[sha256.getDigestSize()];
    sha256.update(bytesWithSalt, 0, bytesWithSalt.length);
    sha256.doFinal(usernameHashBytes, 0);

    return usernameHashBytes;
}

From source file:dorkbox.util.Sys.java

License:Apache License

/**
 * gets the SHA256 hash + SALT of the specified username, as UTF-16
 *//*from   ww w.j ava2  s.  c o  m*/
public static byte[] getSha256WithSalt(String username, byte[] saltBytes) {
    if (username == null) {
        return null;
    }

    byte[] charToBytes = Sys.charToBytes(username.toCharArray());
    byte[] userNameWithSalt = Sys.concatBytes(charToBytes, saltBytes);

    SHA256Digest sha256 = new SHA256Digest();
    byte[] usernameHashBytes = new byte[sha256.getDigestSize()];
    sha256.update(userNameWithSalt, 0, userNameWithSalt.length);
    sha256.doFinal(usernameHashBytes, 0);

    return usernameHashBytes;
}

From source file:dorkbox.util.Sys.java

License:Apache License

/**
 * gets the SHA256 hash of the specified string, as UTF-16
 */// w  w w  .  ja v a 2  s  .c o m
public static byte[] getSha256(String string) {
    byte[] charToBytes = Sys.charToBytes(string.toCharArray());

    SHA256Digest sha256 = new SHA256Digest();
    byte[] usernameHashBytes = new byte[sha256.getDigestSize()];
    sha256.update(charToBytes, 0, charToBytes.length);
    sha256.doFinal(usernameHashBytes, 0);

    return usernameHashBytes;
}

From source file:edu.biu.scapi.primitives.hash.bc.BcSHA256.java

License:Open Source License

/** 
 * Passes the digest SHA256 of BC to the super class which does the hash computation. 
 *//*from w  w w  . ja  va  2s  . com*/
public BcSHA256() {
    //passes the digest SHA256 of BC. 
    super(new SHA256Digest());
}

From source file:edu.tamu.tcat.crypto.bouncycastle.internal.DigestTypeMap.java

License:Apache License

public static Digest getDigest(DigestType type) {
    Objects.requireNonNull(type);
    switch (type) {
    case SHA1:/*from  w w w.j av a2  s  .com*/
        return new SHA1Digest();
    case SHA224:
        return new SHA224Digest();
    case SHA256:
        return new SHA256Digest();
    case SHA384:
        return new SHA384Digest();
    case SHA512:
        return new SHA512Digest();
    default:
        throw new IllegalArgumentException();
    }
}