use of com.icodici.crypto.digest.Sha512 in project universa by UniversaBlockchain.
the class SymmetricKeyTest method testHashes.
@Test
public void testHashes() throws Exception {
byte[] valid = Hex.decode("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61" + " f20015ad");
Digest d = new Sha256();
d.update('a');
d.update("bc".getBytes());
assertArrayEquals(valid, d.digest());
// And SHA512 too
valid = Hex.decode("ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a " + "2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e " + "2a9ac94fa54ca49f");
assertArrayEquals(valid, new Sha512().digest("abc"));
}
use of com.icodici.crypto.digest.Sha512 in project universa by UniversaBlockchain.
the class ExtendedSignature method sign.
/**
* Sign the data with a given key.
*
* @param key is {@link PrivateKey} to sign with.
* @param data to be sign with key.
* @param savePublicKey if true key will stored in the {@link ExtendedSignature}.
*
* @return binary signature
*/
public static byte[] sign(PrivateKey key, byte[] data, boolean savePublicKey) {
try {
Binder targetSignatureBinder = Binder.fromKeysValues("key", keyId(key), "sha512", new Sha512().digest(data), "sha3_384", new Sha3_384().digest(data), "created_at", ZonedDateTime.now());
if (savePublicKey)
targetSignatureBinder.put("pub_key", key.getPublicKey().pack());
byte[] targetSignature = Boss.pack(targetSignatureBinder);
Binder result = Binder.fromKeysValues("exts", targetSignature, "sign", key.sign(targetSignature, HashType.SHA512), "sign2", key.sign(targetSignature, HashType.SHA3_384));
return Boss.pack(result);
} catch (EncryptionError e) {
throw new RuntimeException("signature failed", e);
}
}
use of com.icodici.crypto.digest.Sha512 in project universa by UniversaBlockchain.
the class ExtendedSignature method verify.
/**
* Unpack and the extended signature. On success, returns instance of the {@link ExtendedSignature} with a decoded
* timestamp, {@link #getCreatedAt()}
*
* @param key to verify signature with
* @param signature the binary extended signature
* @param data signed data
*
* @return null if the signature is invalud, {@link ExtendedSignature} instance on success.
*/
public static ExtendedSignature verify(PublicKey key, byte[] signature, byte[] data) {
try {
Binder src = Boss.unpack(signature);
ExtendedSignature es = new ExtendedSignature();
byte[] exts = src.getBinaryOrThrow("exts");
boolean isSignValid = key.verify(exts, src.getBinaryOrThrow("sign"), HashType.SHA512);
boolean isSign2Valid = true;
byte[] sign2bin = null;
try {
sign2bin = src.getBinaryOrThrow("sign2");
} catch (IllegalArgumentException e) {
sign2bin = null;
}
if (sign2bin != null)
isSign2Valid = key.verify(exts, sign2bin, HashType.SHA3_384);
if (isSignValid && isSign2Valid) {
Binder b = Boss.unpack(exts);
es.keyId = b.getBytesOrThrow("key");
es.createdAt = b.getZonedDateTimeOrThrow("created_at");
es.signature = signature;
es.publicKey = null;
try {
byte[] publicKeyBytes = b.getBinaryOrThrow("pub_key");
es.publicKey = new PublicKey(publicKeyBytes);
} catch (IllegalArgumentException e) {
es.publicKey = null;
}
Bytes hash = b.getBytesOrThrow("sha512");
Bytes dataHash = new Bytes(new Sha512().digest(data));
boolean isHashValid = hash.equals(dataHash);
Bytes hash2 = null;
boolean isHash2Valid = true;
try {
hash2 = b.getBytesOrThrow("sha3_384");
} catch (IllegalArgumentException e) {
hash2 = null;
}
if (hash2 != null) {
Bytes dataHash2 = new Bytes(new Sha3_384().digest(data));
isHash2Valid = hash2.equals(dataHash2);
}
if (isHashValid && isHash2Valid)
return es;
}
} catch (EncryptionError encryptionError) {
encryptionError.printStackTrace();
}
return null;
}