@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]; } }
@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; }
/** * Sign a hash with the private key of this key pair. * @param transactionHash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] transactionHash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(transactionHash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
/** * Sign a digest with this key. * * @param hash arbitrary data * @return signature */ public byte[] sign(byte[] hash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(priv, 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 null; }
public BigInteger[] ecdsaSign(PrivateKey privateKey, ByteString message) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(digest)); ECPrivateKeyParameters ecdhPrivateKeyParameters; try { ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(privateKey.getEncoded())); } catch (IOException e) { logger.error("ECDSA sign load private key exception", e); throw new RuntimeException(e); } signer.init(true, ecdhPrivateKeyParameters); return signer.generateSignature(message.toByteArray()); }
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(); }
static String signData(String input, byte[] key) throws BlockIOException { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); X9ECParameters params = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); BigInteger priv = new BigInteger(1, key); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(priv, ecParams); signer.init(true, privKey); BigInteger[] sigs = signer.generateSignature(fromHex(input)); BigInteger r = sigs[0]; BigInteger s = sigs[1]; // BIP62: "S must be less than or equal to half of the Group Order N" BigInteger overTwo = params.getN().shiftRight(1); if (s.compareTo(overTwo) == 1) { s = params.getN().subtract(s); } try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return toHex(bos.toByteArray()); } catch (IOException e) { throw new BlockIOException("That should never happen... File an issue report."); // Cannot happen. } }
/** * Creates a signature for the supplied contents using the private key * * @param contents Contents to be signed * @return ECDSA signature * @throws ECException Unable to create signature */ public ECDSASignature createSignature(byte[] contents) throws ECException { if (privKey == null) throw new IllegalStateException("No private key available"); // // Get the double SHA-256 hash of the signed contents // byte[] contentsHash = Utils.doubleDigest(contents); // // Create the signature // BigInteger[] sigs; try { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(privKey, ecParams); signer.init(true, privKeyParams); sigs = signer.generateSignature(contentsHash); } catch (RuntimeException exc) { throw new ECException("Exception while creating signature", exc); } // // Create a canonical signature by adjusting the S component to be less than or equal to // half the curve order. // if (sigs[1].compareTo(HALF_CURVE_ORDER) > 0) sigs[1] = ecParams.getN().subtract(sigs[1]); return new ECDSASignature(sigs[0], sigs[1]); }
protected DSA createDSAImpl(short hashAlgorithm) { return new ECDSASigner(new HMacDSAKCalculator(TlsUtils.createHash(hashAlgorithm))); }
protected DSA createDSAImpl(short hashAlgorithm) { return new DSASigner(new HMacDSAKCalculator(TlsUtils.createHash(hashAlgorithm))); }
private void doTestHMACDetDSASample(Digest digest, DSAPrivateKeyParameters privKey, BigInteger r, BigInteger s) { doTestHMACDetECDSA(new DSASigner(new HMacDSAKCalculator(digest)), digest, SAMPLE, privKey, r, s); }
private void doTestHMACDetDSATest(Digest digest, DSAPrivateKeyParameters privKey, BigInteger r, BigInteger s) { doTestHMACDetECDSA(new DSASigner(new HMacDSAKCalculator(digest)), digest, TEST, privKey, r, s); }
private void doTestHMACDetECDSASample(Digest digest, ECPrivateKeyParameters privKey, BigInteger r, BigInteger s) { doTestHMACDetECDSA(new ECDSASigner(new HMacDSAKCalculator(digest)), digest, SAMPLE, privKey, r, s); }
private void doTestHMACDetECDSATest(Digest digest, ECPrivateKeyParameters privKey, BigInteger r, BigInteger s) { doTestHMACDetECDSA(new ECDSASigner(new HMacDSAKCalculator(digest)), digest, TEST, privKey, r, s); }
public detDSA() { super(new SHA1Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA1Digest()))); }
public detDSA224() { super(new SHA224Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA224Digest()))); }
public detDSA256() { super(new SHA256Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA256Digest()))); }
public detDSA384() { super(new SHA384Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA384Digest()))); }
public detDSA512() { super(new SHA512Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA512Digest()))); }
public ecDetDSA() { super(new SHA1Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA1Digest())), new StdDSAEncoder()); }
public ecDetDSA224() { super(new SHA224Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA224Digest())), new StdDSAEncoder()); }
public ecDetDSA256() { super(new SHA256Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())), new StdDSAEncoder()); }
public ecDetDSA384() { super(new SHA384Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA384Digest())), new StdDSAEncoder()); }
public ecDetDSA512() { super(new SHA512Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA512Digest())), new StdDSAEncoder()); }