use of org.bouncycastle.crypto.params.ECDomainParameters 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.");
}
}
use of org.bouncycastle.crypto.params.ECDomainParameters in project jruby-openssl by jruby.
the class PKCS10Request method generatePublicKey.
public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
final KeySpec keySpec;
final KeyFactory keyFactory;
if (keyParams instanceof RSAKeyParameters) {
RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
keyFactory = SecurityHelper.getKeyFactory("RSA");
return keyFactory.generatePublic(keySpec);
} else if (keyParams instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
DSAParameters params = dsa.getParameters();
keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
keyFactory = SecurityHelper.getKeyFactory("DSA");
return keyFactory.generatePublic(keySpec);
} else if (keyParams instanceof ECPublicKeyParameters) {
ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
ECDomainParameters ecParams = ec.getParameters();
ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
// NOTE: likely to fail if non BC factory picked up :
keySpec = new ECPublicKeySpec(ec.getQ(), params);
keyFactory = SecurityHelper.getKeyFactory("EC");
return keyFactory.generatePublic(keySpec);
} else {
throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
}
}
Aggregations