Search in sources :

Example 6 with HMacDSAKCalculator

use of org.spongycastle.crypto.signers.HMacDSAKCalculator in project SmartMesh_Android by SmartMeshFoundation.

the class Sign2 method sign.

private static ECDSASignature sign(byte[] transactionHash, BigInteger privateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, CURVE);
    signer.init(true, privKey);
    BigInteger[] components = signer.generateSignature(transactionHash);
    return new ECDSASignature(components[0], components[1]).toCanonicalised();
}
Also used : HMacDSAKCalculator(org.spongycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.spongycastle.crypto.params.ECPrivateKeyParameters) ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 7 with HMacDSAKCalculator

use of org.spongycastle.crypto.signers.HMacDSAKCalculator in project toshi-android-client by toshiapp.

the class ECKey method doSign.

/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null)
        throw new MissingPrivateKeyException();
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) HMacDSAKCalculator(org.spongycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.spongycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) Signature(java.security.Signature) BigInteger(java.math.BigInteger)

Aggregations

BigInteger (java.math.BigInteger)7 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)7 ECDSASigner (org.spongycastle.crypto.signers.ECDSASigner)7 HMacDSAKCalculator (org.spongycastle.crypto.signers.HMacDSAKCalculator)7 ECPrivateKeyParameters (org.spongycastle.crypto.params.ECPrivateKeyParameters)5 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 SignatureException (java.security.SignatureException)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)4 ISignature (org.aion.crypto.ISignature)1