use of org.bouncycastle.crypto.signers.HMacDSAKCalculator in project web3sdk by FISCO-BCOS.
the class Sign 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();
}
use of org.bouncycastle.crypto.signers.HMacDSAKCalculator in project OsmAnd-tools by osmandapp.
the class SigningUtils method signData.
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) {
// Cannot happen.
throw new BlockIOException("That should never happen... File an issue report.");
}
}
Aggregations