public Object clone() throws CloneNotSupportedException { BCMessageDigest d = (BCMessageDigest)super.clone(); d.digest = new SHA3Digest((SHA3Digest)digest); return d; }
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; }
private static void initDigesters() { digesters.put("Blake2b", Blake2bDigest.class); digesters.put("GOST3411", GOST3411Digest.class); digesters.put("Keccak", KeccakDigest.class); digesters.put("MD2", MD2Digest.class); digesters.put("MD4", MD4Digest.class); digesters.put("MD5", MD5Digest.class); digesters.put("RIPEMD128", RIPEMD128Digest.class); digesters.put("RIPEMD160", RIPEMD160Digest.class); digesters.put("RIPEMD256", RIPEMD256Digest.class); digesters.put("RIPEMD320", RIPEMD320Digest.class); digesters.put("SHA1", SHA1Digest.class); digesters.put("SHA224", SHA224Digest.class); digesters.put("SHA256", SHA256Digest.class); digesters.put("SHA384", SHA384Digest.class); digesters.put("SHA3-512", SHA3Digest.class); digesters.put("SHA3-256", SHA3Digest.class); digesters.put("SHA3-224", SHA3Digest.class); digesters.put("SHA3-384", SHA3Digest.class); digesters.put("SHA512", SHA512Digest.class); digesters.put("SHAKE-128", SHAKEDigest.class); digesters.put("SHAKE-256", SHAKEDigest.class); digesters.put("Skein256", SkeinDigest.class); digesters.put("Skein512", SkeinDigest.class); digesters.put("Skein1024", SkeinDigest.class); digesters.put("SM3", SM3Digest.class); digesters.put("Tiger", TigerDigest.class); digesters.put("Whirlpool", WhirlpoolDigest.class); }
private Digest getHashDigest() { if ("SHA3".equals(hashAlgorithm)) { return new SHA3Digest(); } else { // Default to SHA2 return new SHA256Digest(); } }
/** * 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(String.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())); }
/** * 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(String.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(String.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(String.format("The chaincode directory \"%s\" has no files", projectPath)); } return hashBuilder.toString(); }
@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); }
@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)); }
/** * Signs the server's public ID with the cluster key. */ public static String[] getClusterSignature(String clusterKey) { LinkedList<String> rsvValues = new LinkedList<>(); while (rsvValues.isEmpty()) { // Hash the public key SHA3Digest sha3 = new SHA3Digest(256); sha3.reset(); sha3.update(Secp256k1.getPublicKey(myKey), 0, Secp256k1.getPublicKey(myKey).length); byte[] hashedBytes = new byte[256 / 8]; sha3.doFinal(hashedBytes, 0); // Sign it. byte[][] signature = Secp256k1.signTransaction(hashedBytes, ByteUtilities.toByteArray(clusterKey)); Arrays.asList(signature) .forEach(sigValue -> rsvValues.add(ByteUtilities.toHexString(sigValue))); // Recovery ID is bad, try again. if (rsvValues.get(2).equalsIgnoreCase("ff")) { LOGGER.debug("Problem getting signature, V is invalid."); LOGGER.debug(rsvValues.toString()); rsvValues.clear(); } } return rsvValues.toArray(new String[rsvValues.size()]); }
public void performTest() { testDigest(new SHA3Digest(), digests288); testDigest(new SHA3Digest(224), digests224); testDigest(new SHA3Digest(256), digests256); testDigest(new SHA3Digest(384), digests384); testDigest(new SHA3Digest(512), digests512); testMac(new SHA3Digest(224), macKeys, macData, mac224, trunc224); testMac(new SHA3Digest(256), macKeys, macData, mac256, trunc256); testMac(new SHA3Digest(384), macKeys, macData, mac384, trunc384); testMac(new SHA3Digest(512), macKeys, macData, mac512, trunc512); }
public DigestSHA3(int size) { super(new SHA3Digest(size)); }
public HashMac224() { super(new HMac(new SHA3Digest(224))); }
public HashMac256() { super(new HMac(new SHA3Digest(256))); }
public HashMac384() { super(new HMac(new SHA3Digest(384))); }
public HashMac512() { super(new HMac(new SHA3Digest(512))); }
/** * Hashes a string using SHA3 to a specified strength. * @param input the string to hash * @return The hashed string as a byte array * @throws UnsupportedEncodingException */ public static byte[] hashToBytes(final String input) throws UnsupportedEncodingException { SHA3Digest sha3Digest = new SHA3Digest(HASH_STRENGTH); sha3Digest.update(input.getBytes(UTF_8), 0, input.length()); byte[] result = new byte[sha3Digest.getDigestSize()]; sha3Digest.doFinal(result, 0); return result; }
/** * Add a server to our known hosts. * * <p>Returns true or false depending on whether the server's signature is acceptable or not. */ public boolean addServer(Server server, boolean wasHeartbeat) { LOGGER.debug("Attempting to add a server: " + server); LOGGER.debug("Is a heartbeat: " + wasHeartbeat); if (!getThisServer().equals(server)) { server.setOriginator(false); } byte[] data = ByteUtilities.toByteArray(server.getServerId()); // Hash the public key SHA3Digest sha3 = new SHA3Digest(256); sha3.reset(); sha3.update(data, 0, data.length); byte[] hashedBytes = new byte[256 / 8]; sha3.doFinal(hashedBytes, 0); String recoveredPublicKey = ByteUtilities.toHexString(Secp256k1 .recoverPublicKey(ByteUtilities.toByteArray(server.getSigR()), ByteUtilities.toByteArray(server.getSigS()), ByteUtilities.toByteArray(server.getSigV()), hashedBytes)); String publicKey = ByteUtilities.toHexString(Secp256k1.getPublicKey(ByteUtilities.toByteArray(clusterKey))); if (!publicKey.equalsIgnoreCase(recoveredPublicKey)) { LOGGER.debug("Server doesn't belong to this cluster."); return false; } if (wasHeartbeat) { // If it was a hearbeat then remove the old one so that we replace it, and update the time. if (this.getServers().contains(server)) { this.getServers().remove(server); } server.setLastCommunication(System.currentTimeMillis()); } else { // Otherwise, write in an unknown time if we don't already have an entry for it. server.setLastCommunication(0L); } this.getServers().add(server); return true; }
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())); }
protected Digest cloneDigest(Digest digest) { return new SHA3Digest((SHA3Digest)digest); }