Java Code Examples for org.bouncycastle.crypto.signers.ECDSASigner#init()
The following examples show how to use
org.bouncycastle.crypto.signers.ECDSASigner#init() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BouncyCastleCrypto.java From fabric-api-archive with Apache License 2.0 | 6 votes |
@Override public boolean verify(byte[] hash, byte[] signature, byte[] publicKey) { ASN1InputStream asn1 = new ASN1InputStream(signature); try { ECDSASigner signer = new ECDSASigner(); signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(publicKey), domain)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(hash, r, s); } catch (Exception e) { return false; } finally { try { asn1.close(); } catch (IOException ignored) { } } }
Example 2
Source File: BouncyCastleCrypto.java From fabric-api with Apache License 2.0 | 6 votes |
@Override public boolean verify(byte[] hash, byte[] signature, byte[] publicKey) { ASN1InputStream asn1 = new ASN1InputStream(signature); try { ECDSASigner signer = new ECDSASigner(); signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(publicKey), domain)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(hash, r, s); } catch (Exception e) { return false; } finally { try { asn1.close(); } catch (IOException ignored) { } } }
Example 3
Source File: BouncyCastleCrypto.java From fabric-api-archive with Apache License 2.0 | 6 votes |
@Override public byte[] sign(byte[] hash, byte[] privateKey) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(toCanonicalS(signature[1]))); seq.close(); return baos.toByteArray(); } catch (IOException e) { return new byte[0]; } }
Example 4
Source File: ECKeyPair.java From bop-bitcoin-client with Apache License 2.0 | 6 votes |
@Override public byte[] sign (byte[] hash) throws ValidationException { if ( priv == null ) { throw new ValidationException ("Need private key to sign"); } ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ())); signer.init (true, new ECPrivateKeyParameters (priv, domain)); BigInteger[] signature = signer.generateSignature (hash); ByteArrayOutputStream s = new ByteArrayOutputStream (); try { DERSequenceGenerator seq = new DERSequenceGenerator (s); seq.addObject (new ASN1Integer (signature[0])); seq.addObject (new ASN1Integer (signature[1])); seq.close (); return s.toByteArray (); } catch ( IOException e ) { } return null; }
Example 5
Source File: ECKeyPair.java From WalletCordova with GNU Lesser General Public License v2.1 | 6 votes |
@Override public byte[] sign (byte[] hash) throws ValidationException { if ( priv == null ) { throw new ValidationException ("Need private key to sign"); } ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ())); signer.init (true, new ECPrivateKeyParameters (priv, domain)); BigInteger[] signature = signer.generateSignature (hash); ByteArrayOutputStream s = new ByteArrayOutputStream (); try { DERSequenceGenerator seq = new DERSequenceGenerator (s); seq.addObject (new ASN1Integer (signature[0])); seq.addObject (new ASN1Integer (signature[1])); seq.close (); return s.toByteArray (); } catch ( IOException e ) { } return null; }
Example 6
Source File: ECKey.java From nuls-v2 with MIT License | 5 votes |
/** * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p> * * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be * larger than 520 bytes.</p> * * @param data Hash of the data to verify. * @param signature ASN.1 encoded signature. * @param pub The public key bytes to use. */ public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) { ECDSASigner signer = new ECDSASigner(); ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE); signer.init(false, params); try { return signer.verifySignature(data, signature.r, signature.s); } catch (NullPointerException e) { log.error("Caught NPE inside bouncy castle", e); return false; } }
Example 7
Source File: NamedCurve.java From UAF with Apache License 2.0 | 5 votes |
public static boolean verify(byte[] pub, byte[] dataForSigning, BigInteger[] rs) throws Exception { ECDSASigner signer = new ECDSASigner(); X9ECParameters params = SECNamedCurves.getByName("secp256r1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); ECPublicKeyParameters pubKeyParams = new ECPublicKeyParameters(ecParams .getCurve().decodePoint(pub), ecParams); signer.init(false, pubKeyParams); return signer.verifySignature(dataForSigning, rs[0].abs(), rs[1].abs()); }
Example 8
Source File: NamedCurve.java From UAF with Apache License 2.0 | 5 votes |
public static boolean verifyUsingSecp256k1(byte[] pub, byte[] dataForSigning, BigInteger[] rs) throws Exception { ECDSASigner signer = new ECDSASigner(); X9ECParameters params = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); ECPublicKeyParameters pubKeyParams = new ECPublicKeyParameters(ecParams .getCurve().decodePoint(pub), ecParams); signer.init(false, pubKeyParams); return signer.verifySignature(dataForSigning, rs[0].abs(), rs[1].abs()); }
Example 9
Source File: ECKey.java From bushido-java-core with GNU General Public License v3.0 | 5 votes |
public boolean verify(byte[] message, byte[] signature) throws Exception { ASN1InputStream asn1 = new ASN1InputStream(signature); ECDSASigner signer = new ECDSASigner(); //not for signing... signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(pub), params)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(message, r, s); }
Example 10
Source File: ECKeyPair.java From web3sdk with Apache License 2.0 | 5 votes |
/** * Sign a hash with the private key of this key pair. * * @param hash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] hash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(hash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
Example 11
Source File: K256KeyPair.java From jingtum-lib-java with MIT License | 5 votes |
private static ECDSASignature createECDSASignature(byte[] hash, BigInteger secret) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(secret, SECP256K1.params()); signer.init(true, privKey); BigInteger[] sigs = signer.generateSignature(hash); BigInteger r = sigs[0], s = sigs[1]; BigInteger otherS = SECP256K1.order().subtract(s); if (s.compareTo(otherS) == 1) { s = otherS; } return new ECDSASignature(r, s); }
Example 12
Source File: K256KeyPair.java From jingtum-lib-java with MIT License | 5 votes |
public static boolean verify(byte[] data, byte[] sigBytes, BigInteger pub) { ECDSASignature signature = ECDSASignature.decodeFromDER(sigBytes); if (signature == null) { return false; } ECDSASigner signer = new ECDSASigner(); ECPoint pubPoint = SECP256K1.curve().decodePoint(pub.toByteArray()); ECPublicKeyParameters params = new ECPublicKeyParameters(pubPoint, SECP256K1.params()); signer.init(false, params); return signer.verifySignature(data, signature.r, signature.s); }
Example 13
Source File: ECKey.java From nuls-v2 with MIT License | 5 votes |
/** * 用私钥对数据进行签名 * * @param input 需签名数据 * @param privateKeyForSigning 私钥 * @return byte[] 签名 */ protected byte[] doSign(byte[] input, BigInteger privateKeyForSigning) { HexUtil.checkNotNull(privateKeyForSigning); ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(input); return new ECDSASignature(components[0], components[1]).toCanonicalised().encodeToDER(); }
Example 14
Source File: NamedCurve.java From UAF with Apache License 2.0 | 5 votes |
/** * UAF_ALG_SIGN_SECP256R1_ECDSA_SHA256_RAW 0x01 An ECDSA signature on the * NIST secp256r1 curve which MUST have raw R and S buffers, encoded in * big-endian order. I.e. [R (32 bytes), S (32 bytes)] * * @param priv * - Private key * @param input * - Data to sign * @return BigInteger[] - [R,S] */ public static BigInteger[] signAndFromatToRS(PrivateKey priv, byte[] input) { X9ECParameters params = SECNamedCurves.getByName("secp256r1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); if (priv == null) throw new IllegalStateException( "This ECKey does not have the private key necessary for signing."); ECDSASigner signer = new ECDSASigner(); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters( ((ECPrivateKey) priv).getS(), ecParams); signer.init(true, privKey); BigInteger[] sigs = signer.generateSignature(input); return sigs; }
Example 15
Source File: ECKey.java From bushido-java-core with GNU General Public License v3.0 | 5 votes |
public byte[] sign(byte[] message) throws Exception { if (priv == null) { throw new Exception("Unable to sign"); } ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(priv, params)); BigInteger[] signature = signer.generateSignature(message); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DERSequenceGenerator seqGen = new DERSequenceGenerator(outputStream); seqGen.addObject(new ASN1Integer(signature[0])); seqGen.addObject(new ASN1Integer(signature[1])); seqGen.close(); return outputStream.toByteArray(); }
Example 16
Source File: ECCurvePoint.java From InflatableDonkey with MIT License | 5 votes |
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) { ECDomainParameters ecDomainParameters = ECAssistant.ecDomainParametersFrom(x9ECParameters); ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(Q, ecDomainParameters); ECDSASigner signer = new ECDSASigner(); signer.init(false, ecPublicKeyParameters); return signer.verifySignature(message, r, s); }
Example 17
Source File: ECKey.java From javasdk with GNU Lesser General Public License v3.0 | 5 votes |
/** * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p> * <p> * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be * larger than 520 bytes.</p> * * @param data Hash of the data to verify. * @param signature signature. * @param pub The public key bytes to use. * @return - */ public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) { ECDSASigner signer = new ECDSASigner(); ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE); signer.init(false, params); try { return signer.verifySignature(data, signature.r, signature.s); } catch (NullPointerException npe) { // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures. // Those signatures are inherently invalid/attack sigs so we just fail them here rather than crash the thread. logger.error("Caught NPE inside bouncy castle, " + npe); return false; } }
Example 18
Source File: SECP256K1.java From besu with Apache License 2.0 | 5 votes |
private static Signature signDefault(final Bytes32 dataHash, final KeyPair keyPair) { final ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); final ECPrivateKeyParameters privKey = new ECPrivateKeyParameters( keyPair.getPrivateKey().getEncodedBytes().toUnsignedBigInteger(), CURVE); signer.init(true, privKey); final BigInteger[] components = signer.generateSignature(dataHash.toArrayUnsafe()); return normaliseSignature(components[0], components[1], keyPair.getPublicKey(), dataHash); }
Example 19
Source File: SECP256K1.java From incubator-tuweni with Apache License 2.0 | 5 votes |
/** * Verifies the given ECDSA signature against the message bytes using the public key bytes. * * @param hash The keccak256 hash of the data to verify. * @param signature The signature. * @param publicKey The public key. * @return True if the verification is successful. */ public static boolean verifyHashed(byte[] hash, Signature signature, PublicKey publicKey) { ECDSASigner signer = new ECDSASigner(); Bytes toDecode = Bytes.wrap(Bytes.of((byte) 4), publicKey.bytes()); ECPublicKeyParameters params = new ECPublicKeyParameters(Parameters.CURVE.getCurve().decodePoint(toDecode.toArray()), Parameters.CURVE); signer.init(false, params); try { return signer.verifySignature(hash, signature.r, signature.s); } catch (NullPointerException e) { // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures. Those signatures // are inherently invalid/attack sigs so we just fail them here rather than crash the thread. return false; } }
Example 20
Source File: Signature.java From evt4j with MIT License | 3 votes |
public static boolean verifyHash(byte[] hash, @NotNull Signature signature, @NotNull PublicKey publicKey) { checkHashLength(hash); ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPublicKeyParameters publicKeyParams = new ECPublicKeyParameters(publicKey.getPoint().get(), // ECPoint ECKey.CURVE); signer.init(false, publicKeyParams); return signer.verifySignature(hash, signature.getR(), signature.getS()); }