org.bouncycastle.crypto.digests.SHA3Digest Java Examples

The following examples show how to use org.bouncycastle.crypto.digests.SHA3Digest. 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 File: LibraKeyFactory.java    From jlibra with Apache License 2.0 6 votes vote down vote up
public LibraKeyFactory(Seed seed) {
    byte[] data = new byte[32];
    SHA3Digest sha3 = new SHA3Digest(256);
    HMac mac = new HMac(sha3);
    mac.init(new KeyParameter(MASTER_KEY_SALT));
    mac.update(seed.getData(), 0, seed.getData().length);
    mac.doFinal(data, 0);

    this.master = new Master(data);
}
 
Example #2
Source File: LibraKeyFactory.java    From jlibra with Apache License 2.0 6 votes vote down vote up
public ExtendedPrivKey privateChild(ChildNumber childNumber) {
    byte[] secretKey = new byte[32];
    byte[] info = createInfo(childNumber);
    SHA3Digest sha3 = new SHA3Digest(256);
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(sha3);
    hkdf.init(HKDFParameters.skipExtractParameters(master.getData(), info));
    hkdf.generateBytes(secretKey, 0, 32);

    return new ExtendedPrivKey(new SecretKey(ByteArray.from(secretKey)));
}
 
Example #3
Source File: EncryptionUtil.java    From Groza with Apache License 2.0 6 votes vote down vote up
public static String getSha3Hash(String data) {
    String trimmedData = trimNewLines(data);
    byte[] dataBytes = trimmedData.getBytes();
    SHA3Digest md = new SHA3Digest(256);
    md.reset();
    md.update(dataBytes, 0, dataBytes.length);
    byte[] hashedBytes = new byte[256 / 8];
    md.doFinal(hashedBytes, 0);
    String sha3Hash = ByteUtils.toHexString(hashedBytes);
    return sha3Hash;
}
 
Example #4
Source File: Sha3Hash.java    From nuls-v2 with MIT License 6 votes vote down vote up
public static String sha3(byte[] bytes, int bitLength) {
    Digest digest = new SHA3Digest(bitLength);
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return HexUtil.encode(rsData);
}
 
Example #5
Source File: Sha3Hash.java    From nuls-v2 with MIT License 6 votes vote down vote up
public static byte[] sha3bytes(byte[] bytes, int bitLength) {
    Digest digest = new SHA3Digest(bitLength);
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return rsData;
}
 
Example #6
Source File: EncryptionUtil.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
public static String getSha3Hash(String data) {
    String trimmedData = trimNewLines(data);
    byte[] dataBytes = trimmedData.getBytes();
    SHA3Digest md = new SHA3Digest(256);
    md.reset();
    md.update(dataBytes, 0, dataBytes.length);
    byte[] hashedBytes = new byte[256 / 8];
    md.doFinal(hashedBytes, 0);
    String sha3Hash = ByteUtils.toHexString(hashedBytes);
    return sha3Hash;
}
 
Example #7
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
private Digest getHashDigest() {
    if ("SHA3".equals(hashAlgorithm)) {
        return new SHA3Digest();
    } else {
        // Default to SHA2
        return new SHA256Digest();
    }
}
 
Example #8
Source File: Utils.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Generate parameter hash for the given chaincode path,func and args
 *
 * @param path Chaincode path
 * @param func Chaincode function name
 * @param args List of arguments
 * @return hash of path, func and args
 */
public static String generateParameterHash(String path, String func, List<String> args) {
    logger.debug(format("GenerateParameterHash : path=%s, func=%s, args=%s", path, func, args));

    // Append the arguments
    StringBuilder param = new StringBuilder(path);
    param.append(func);
    args.forEach(param::append);

    // Compute the hash

    return Hex.toHexString(hash(param.toString().getBytes(UTF_8), new SHA3Digest()));
}
 
Example #9
Source File: Utils.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Generate hash of a chaincode directory
 *
 * @param rootDir      Root directory
 * @param chaincodeDir Channel code directory
 * @param hash         Previous hash (if any)
 * @return hash of the directory
 * @throws IOException
 */
public static String generateDirectoryHash(String rootDir, String chaincodeDir, String hash) throws IOException {
    // Generate the project directory
    Path projectPath = null;
    if (rootDir == null) {
        projectPath = Paths.get(chaincodeDir);
    } else {
        projectPath = Paths.get(rootDir, chaincodeDir);
    }

    File dir = projectPath.toFile();
    if (!dir.exists() || !dir.isDirectory()) {
        throw new IOException(format("The chaincode path \"%s\" is invalid", projectPath));
    }

    StringBuilder hashBuilder = new StringBuilder(hash);
    Files.walk(projectPath)
            .sorted(Comparator.naturalOrder())
            .filter(Files::isRegularFile)
            .map(Path::toFile)
            .forEach(file -> {
                try {
                    byte[] buf = readFile(file);
                    byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8));
                    hashBuilder.setLength(0);
                    hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest())));
                } catch (IOException ex) {
                    throw new RuntimeException(format("Error while reading file %s", file.getAbsolutePath()), ex);
                }
            });

    // If original hash and final hash are the same, it indicates that no new contents were found
    if (hashBuilder.toString().equals(hash)) {
        throw new IOException(format("The chaincode directory \"%s\" has no files", projectPath));
    }
    return hashBuilder.toString();
}
 
Example #10
Source File: UtilsTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateParameterHash() {
    List<String> args = new ArrayList<>();
    args.add("a");
    args.add("b");
    String hash = Utils.generateParameterHash("mypath", "myfunc", args);
    Assert.assertEquals(Hex.toHexString(Utils.hash("mypathmyfuncab".getBytes(UTF_8), new SHA3Digest())), hash);
}
 
Example #11
Source File: UtilsTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testHash() {

    byte[] input = "TheQuickBrownFox".getBytes(UTF_8);
    String expectedHash = "feb69c5c360a15802de6af23a3f5622da9d96aff2be78c8f188cce57a3549db6";

    byte[] hash = Utils.hash(input, new SHA3Digest());
    Assert.assertEquals(expectedHash, Hex.toHexString(hash));
}
 
Example #12
Source File: BouncyCastleHasher.java    From hash-bench with MIT License 4 votes vote down vote up
public static final void register(final Map<String, Hasher> hashers) {
  hashers.put(BouncyCastleHasher.GOST,
          new BouncyCastleHasher(new GOST3411Digest()));
  hashers.put(BouncyCastleHasher.MD2,
          new BouncyCastleHasher(new MD2Digest()));
  hashers.put(BouncyCastleHasher.MD4,
          new BouncyCastleHasher(new MD4Digest()));
  hashers.put(BouncyCastleHasher.MD5,
          new BouncyCastleHasher(new MD5Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD128,
          new BouncyCastleHasher(new RIPEMD128Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD160,
          new BouncyCastleHasher(new RIPEMD160Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD256,
          new BouncyCastleHasher(new RIPEMD256Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD320,
          new BouncyCastleHasher(new RIPEMD320Digest()));
  hashers.put(BouncyCastleHasher.SHA1,
          new BouncyCastleHasher(new SHA1Digest()));
  hashers.put(BouncyCastleHasher.SHA224,
          new BouncyCastleHasher(new SHA224Digest()));
  hashers.put(BouncyCastleHasher.SHA256,
          new BouncyCastleHasher(new SHA256Digest()));
  hashers.put(BouncyCastleHasher.SHA3,
          new BouncyCastleHasher(new SHA3Digest()));
  hashers.put(BouncyCastleHasher.SHA384,
          new BouncyCastleHasher(new SHA384Digest()));
  hashers.put(BouncyCastleHasher.SHA512,
          new BouncyCastleHasher(new SHA512Digest()));
  hashers.put(BouncyCastleHasher.SHA512_T,
          new BouncyCastleHasher(new SHA512tDigest(7 * 8)));
  hashers.put(BouncyCastleHasher.SKEIN1024, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_1024, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SKEIN256, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_256, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SKEIN512, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_512, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SM3,
          new BouncyCastleHasher(new SM3Digest()));
  hashers.put(BouncyCastleHasher.TIGER,
          new BouncyCastleHasher(new TigerDigest()));
  hashers.put(BouncyCastleHasher.WHIRLPOOL2,
          new BouncyCastleHasher(new WhirlpoolDigest()));
}