Search in sources :

Example 1 with Sha3_384

use of com.icodici.crypto.digest.Sha3_384 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);
    }
}
Also used : Binder(net.sergeych.tools.Binder) Sha512(com.icodici.crypto.digest.Sha512) Sha3_384(com.icodici.crypto.digest.Sha3_384)

Example 2 with Sha3_384

use of com.icodici.crypto.digest.Sha3_384 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;
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) Sha512(com.icodici.crypto.digest.Sha512) Sha3_384(com.icodici.crypto.digest.Sha3_384)

Aggregations

Sha3_384 (com.icodici.crypto.digest.Sha3_384)2 Sha512 (com.icodici.crypto.digest.Sha512)2 Binder (net.sergeych.tools.Binder)2 Bytes (net.sergeych.utils.Bytes)1