Search in sources :

Example 1 with SHA3Digest

use of org.bouncycastle.crypto.digests.SHA3Digest in project thingsboard by thingsboard.

the class EncryptionUtil method getSha3Hash.

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;
}
Also used : SHA3Digest(org.bouncycastle.crypto.digests.SHA3Digest)

Example 2 with SHA3Digest

use of org.bouncycastle.crypto.digests.SHA3Digest in project fabric-sdk-java by hyperledger.

the class UtilsTest method testGenerateParameterHash.

@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);
}
Also used : SHA3Digest(org.bouncycastle.crypto.digests.SHA3Digest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 3 with SHA3Digest

use of org.bouncycastle.crypto.digests.SHA3Digest in project fabric-sdk-java by hyperledger.

the class UtilsTest method testHash.

@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));
}
Also used : SHA3Digest(org.bouncycastle.crypto.digests.SHA3Digest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 4 with SHA3Digest

use of org.bouncycastle.crypto.digests.SHA3Digest in project fabric-sdk-java by hyperledger.

the class Utils method generateDirectoryHash.

/**
 * 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();
}
Also used : Path(java.nio.file.Path) SHA3Digest(org.bouncycastle.crypto.digests.SHA3Digest) IOException(java.io.IOException) Files(java.nio.file.Files) File(java.io.File)

Aggregations

SHA3Digest (org.bouncycastle.crypto.digests.SHA3Digest)4 ByteString (com.google.protobuf.ByteString)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1