use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project XobotOS by xamarin.
the class ECUtil method generatePrivateKeyParameter.
public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
if (key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
ECParameterSpec s = k.getParameters();
if (s == null) {
s = ProviderUtil.getEcImplicitlyCa();
}
return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
}
throw new InvalidKeyException("can't identify EC private key.");
}
use of org.bouncycastle.crypto.params.ECPrivateKeyParameters 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.params.ECPrivateKeyParameters in project nuls by nuls-io.
the class Cipher method initEnc.
public ECPoint initEnc(SM2 sm2, ECPoint userKey) {
AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.generateKeyPair();
ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key.getPrivate();
ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic();
BigInteger k = ecpriv.getD();
ECPoint c1 = ecpub.getQ();
this.p2 = userKey.multiply(k);
reset();
return c1;
}
use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project xipki by xipki.
the class DSAPlainDigestSigner method init.
@Override
public void init(boolean forSigning, CipherParameters parameters) {
this.forSigning = forSigning;
AsymmetricKeyParameter param;
if (parameters instanceof ParametersWithRandom) {
param = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
} else {
param = (AsymmetricKeyParameter) parameters;
}
ParamUtil.requireNonNull("param", param);
if (param instanceof ECPublicKeyParameters) {
keyBitLen = ((ECPublicKeyParameters) param).getParameters().getCurve().getFieldSize();
} else if (param instanceof ECPrivateKeyParameters) {
keyBitLen = ((ECPrivateKeyParameters) param).getParameters().getCurve().getFieldSize();
} else if (param instanceof DSAPublicKeyParameters) {
keyBitLen = ((DSAPublicKeyParameters) param).getParameters().getQ().bitLength();
} else if (param instanceof DSAPrivateKeyParameters) {
keyBitLen = ((DSAPrivateKeyParameters) param).getParameters().getQ().bitLength();
} else {
throw new IllegalArgumentException("unknown parameters: " + param.getClass().getName());
}
if (forSigning && !param.isPrivate()) {
throw new IllegalArgumentException("Signing Requires Private Key.");
}
if (!forSigning && param.isPrivate()) {
throw new IllegalArgumentException("Verification Requires Public Key.");
}
reset();
dsaSigner.init(forSigning, parameters);
}
use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project jruby-openssl by jruby.
the class PKeyEC method dsa_sign_asn1.
@JRubyMethod(name = "dsa_sign_asn1")
public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject data) {
try {
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec(getCurveName());
ASN1ObjectIdentifier oid = getCurveOID(getCurveName());
ECNamedDomainParameters domainParams = new ECNamedDomainParameters(oid, params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed());
final ECDSASigner signer = new ECDSASigner();
final ECPrivateKey privKey = (ECPrivateKey) this.privateKey;
signer.init(true, new ECPrivateKeyParameters(privKey.getS(), domainParams));
final byte[] message = data.convertToString().getBytes();
// [r, s]
BigInteger[] signature = signer.generateSignature(message);
// final byte[] r = signature[0].toByteArray();
// final byte[] s = signature[1].toByteArray();
// // ASN.1 encode as: 0x30 len 0x02 rlen (r) 0x02 slen (s)
// final int len = 1 + (1 + r.length) + 1 + (1 + s.length);
//
// final byte[] encoded = new byte[1 + 1 + len]; int i;
// encoded[0] = 0x30;
// encoded[1] = (byte) len;
// encoded[2] = 0x20;
// encoded[3] = (byte) r.length;
// System.arraycopy(r, 0, encoded, i = 4, r.length); i += r.length;
// encoded[i++] = 0x20;
// encoded[i++] = (byte) s.length;
// System.arraycopy(s, 0, encoded, i, s.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ASN1OutputStream asn1 = new ASN1OutputStream(bytes);
ASN1EncodableVector v = new ASN1EncodableVector();
// r
v.add(new ASN1Integer(signature[0]));
// s
v.add(new ASN1Integer(signature[1]));
asn1.writeObject(new DLSequence(v));
return StringHelper.newString(context.runtime, bytes.buffer(), bytes.size());
} catch (IOException ex) {
throw newECError(context.runtime, ex.toString());
} catch (RuntimeException ex) {
throw newECError(context.runtime, ex.toString());
}
}
Aggregations