use of org.spongycastle.crypto.Digest in project aion by aionnetwork.
the class HashUtil method ripemd160.
/**
* Computes the ripemed160 hash fo the given input data.
*
* @param data Message to hash
* @return reipmd160 hash of the message
*/
public static byte[] ripemd160(byte[] data) {
Digest digest = new RIPEMD160Digest();
if (data != null) {
byte[] resBuf = new byte[digest.getDigestSize()];
digest.update(data, 0, data.length);
digest.doFinal(resBuf, 0);
return resBuf;
}
throw new NullPointerException("Can't hash a NULL value");
}
use of org.spongycastle.crypto.Digest in project rskj by rsksmart.
the class HashUtil method ripemd160.
/**
* @param data - message to hash
* @return - reipmd160 hash of the message
*/
public static byte[] ripemd160(byte[] data) {
Digest digest = new RIPEMD160Digest();
if (data != null) {
byte[] resBuf = new byte[digest.getDigestSize()];
digest.update(data, 0, data.length);
digest.doFinal(resBuf, 0);
return resBuf;
}
throw new NullPointerException("Can't hash a NULL value");
}
use of org.spongycastle.crypto.Digest in project universa by UniversaBlockchain.
the class RSAOAEPDigestFactoryTest method getDigestByName.
/**
* Test {@link RSAOAEPDigestFactory#getDigestByName}.
*/
@Test
public void getDigestByName() throws Exception {
Digest dNoDigest1 = RSAOAEPDigestFactory.getDigestByName("");
assertNull(dNoDigest1);
Digest dNoDigest2 = RSAOAEPDigestFactory.getDigestByName("Missing hash");
assertNull(dNoDigest2);
Digest dSHA1 = RSAOAEPDigestFactory.getDigestByName("SHA-1");
assertArrayEquals(resetAndDigest(dSHA1, "".getBytes()), Hex.decode("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
assertArrayEquals(resetAndDigest(dSHA1, "The quick brown fox jumps over the lazy dog".getBytes()), Hex.decode("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"));
Digest dSHA224 = RSAOAEPDigestFactory.getDigestByName("SHA-224");
assertArrayEquals(resetAndDigest(dSHA224, "".getBytes()), Hex.decode("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"));
assertArrayEquals(resetAndDigest(dSHA224, "The quick brown fox jumps over the lazy dog".getBytes()), Hex.decode("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525"));
Digest dSHA256 = RSAOAEPDigestFactory.getDigestByName("SHA-256");
assertArrayEquals(resetAndDigest(dSHA256, "".getBytes()), Hex.decode("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
assertArrayEquals(resetAndDigest(dSHA256, "The quick brown fox jumps over the lazy dog".getBytes()), Hex.decode("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"));
Digest dSHA384 = RSAOAEPDigestFactory.getDigestByName("SHA-384");
assertArrayEquals(resetAndDigest(dSHA384, "".getBytes()), Hex.decode("38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"));
assertArrayEquals(resetAndDigest(dSHA384, "The quick brown fox jumps over the lazy dog".getBytes()), Hex.decode("ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1"));
Digest dSHA512 = RSAOAEPDigestFactory.getDigestByName("SHA-512");
assertArrayEquals(resetAndDigest(dSHA512, "".getBytes()), Hex.decode("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"));
assertArrayEquals(resetAndDigest(dSHA512, "The quick brown fox jumps over the lazy dog".getBytes()), Hex.decode("07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"));
}
use of org.spongycastle.crypto.Digest in project universa by UniversaBlockchain.
the class RSAOAEPPublicKey method checkSignature.
/**
* {@inheritDoc}
*/
@NonNull
@Override
public boolean checkSignature(InputStream input, byte[] signature, HashType hashType, int saltLength) throws IllegalStateException, IOException {
if (state == null) {
throw new IllegalStateException();
} else {
final Digest primaryDigest = hashType.makeDigest();
if (saltLength == MAX_SALT_LENGTH) {
saltLength = getMaxSaltLength(getBitStrength(), primaryDigest.getDigestSize());
}
if (saltLength < 0) {
throw new RuntimeException(String.format("Incorrect salt length %s", saltLength));
}
final Signer signatureChecker = new PSSSigner(RSAEngineFactory.make(), primaryDigest, state.mgf1HashType.makeDigest(), saltLength);
signatureChecker.init(false, new ParametersWithRandom(state.keyParameters, state.rng));
boolean done = false;
while (!done) {
int availableBytes = input.available();
if (availableBytes <= 0) {
done = true;
} else {
byte[] buffer = new byte[availableBytes];
int howManyBytesRead = input.read(buffer);
if (howManyBytesRead <= 0) {
done = true;
} else {
signatureChecker.update(buffer, 0, howManyBytesRead);
}
}
}
return signatureChecker.verifySignature(signature);
}
}
use of org.spongycastle.crypto.Digest in project universa by UniversaBlockchain.
the class OAEPEncodingTest method randomEncodeDecode.
/**
* Make sure the SpongyCastle OAEPEncoding encoding and decoding operations
* do not lose or corrupt data.
* We test it:
* For each hash function we may use with OEAP (like, SHA1 or SHA512),
* and for each RSA key size (among 1024, 2048, 4096)
* we create multiple (NUMBER_OF_RANDOM_ENCRYPTION_KEY_PAIRS) RSA key pairs;
* for each of the key pair we test encryption-decryption cycle
* with NUMBER_OF_RANDOM_ENCRYPTION_DECRYPTION_CYCLES_PER_KEY_PAIR random messages
* (each of the maximum possible size for this cipher configuration)
* and make sure the result matches the original random message.
*
* @throws Exception
*/
@Test
public void randomEncodeDecode() throws Exception {
RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
for (Digest digest : DIGESTS) {
for (int i = 0; i < NUMBER_OF_RANDOM_ENCRYPTION_KEY_PAIRS; i++) {
// Create key pair
SecureRandom rng = new SecureRandom();
int publicExponent = PUBLIC_EXPONENTS[rng.nextInt(PUBLIC_EXPONENTS.length)];
int keySize = KEY_SIZES[rng.nextInt(KEY_SIZES.length)];
keyGen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(publicExponent), new SecureRandom(), keySize, RSA_KEY_CERTAINTY));
AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
RSAKeyParameters publicKey = (RSAKeyParameters) keyPair.getPublic(), privateKey = (RSAKeyParameters) keyPair.getPrivate();
assertEquals(keySize, publicKey.getModulus().bitLength());
// though actually it is sufficient to keysize <= publicKey.getModulus().bitLength()
int maxMessageSize = keySize / 8 - 2 - 2 * digest.getDigestSize(), minMessageSize = 1, messageSize = (maxMessageSize >= minMessageSize) ? rng.nextInt(maxMessageSize - minMessageSize + 1) + minMessageSize : 0;
// messageSize may become negative with too small RSA key size and too large digest.
if (messageSize > 0) {
// For each key pair we do multiple encryption-decryption cycles
for (int j = 0; j < NUMBER_OF_RANDOM_ENCRYPTION_DECRYPTION_CYCLES_PER_KEY_PAIR; j++) {
// Create random message
byte[] message = new byte[messageSize];
rng.nextBytes(message);
AsymmetricBlockCipher encoder = new OAEPEncoding(RSAEngineFactory.make(), digest), decoder = new OAEPEncoding(RSAEngineFactory.make(), digest);
encoder.init(true, publicKey);
decoder.init(false, privateKey);
byte[] encoded = encoder.processBlock(message, 0, message.length);
byte[] decoded = decoder.processBlock(encoded, 0, encoded.length);
// Finally, test the encoding/decoding cycle
String assertMessage = String.format("Digest %s,\n message %s,\n public key %s / %s,\n private key %s / %s", digest, Hex.toHexString(message), publicKey.getExponent(), publicKey.getModulus(), privateKey.getExponent(), privateKey.getModulus());
assertArrayEquals(assertMessage, message, decoded);
}
}
}
}
}
Aggregations