本文整理汇总了Java中org.bouncycastle.crypto.signers.ECDSASigner.generateSignature方法的典型用法代码示例。如果您正苦于以下问题:Java ECDSASigner.generateSignature方法的具体用法?Java ECDSASigner.generateSignature怎么用?Java ECDSASigner.generateSignature使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.signers.ECDSASigner
的用法示例。
在下文中一共展示了ECDSASigner.generateSignature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
@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];
}
}
示例2: curveTest
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
@Test
public void curveTest() {
final X9ECParameters curve = NISTNamedCurves.getByName("P-384");
final ECDomainParameters domainParameters = new ECDomainParameters(
curve.getCurve(), curve.getG(), curve.getN());
final SecureRandom random = new SecureRandom();
final ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(domainParameters, random));
final AsymmetricCipherKeyPair senderPair = gen.generateKeyPair();
final ECDSASigner signer = new ECDSASigner();
signer.init(true, senderPair.getPrivate());
final byte[] message = "The magic words are squeamish ossifrage".getBytes(
Constants.ASCII);
final SHA384Digest digester = new SHA384Digest();
digester.update(message, 0, message.length);
final byte[] digest = new byte[digester.getDigestSize()];
digester.doFinal(digest, 0);
final BigInteger[] signature = signer.generateSignature(digest);
final ECDSASigner verifier = new ECDSASigner();
verifier.init(false, senderPair.getPublic());
assertTrue(verifier.verifySignature(digest, signature[0], signature[1]));
}
示例3: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
@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;
}
示例4: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
default byte[] sign(Account account) {
ECDSASigner signer = new ECDSASigner();
signer.init(true, new ECPrivateKeyParameters(new BigInteger(1, account.privateKey), ECC.secp256r1));
BigInteger[] bi = signer.generateSignature(Digest.sha256(getHashData()));// dna
byte[] signature = new byte[64];
System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[0]), 0, signature, 0, 32);
System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[1]), 0, signature, 32, 32);
return signature;
}
示例5: signAndFromatToRS
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* 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;
}
示例6: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* 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();
}
示例7: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* 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;
}
示例8: ecdsaSign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
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());
}
示例9: signTransaction
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* Sign data using the ECDSA algorithm.
*/
public static byte[][] signTransaction(byte[] data, byte[] privateKey) {
try {
Security.addProvider(new BouncyCastleProvider());
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(SECP256K1);
ECDSASigner ecdsaSigner = new ECDSASigner();
ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN());
ECPrivateKeyParameters privateKeyParms =
new ECPrivateKeyParameters(new BigInteger(1, privateKey), domain);
ParametersWithRandom params = new ParametersWithRandom(privateKeyParms);
ecdsaSigner.init(true, params);
BigInteger[] sig = ecdsaSigner.generateSignature(data);
LinkedList<byte[]> sigData = new LinkedList<>();
byte[] publicKey = getPublicKey(privateKey);
byte recoveryId = getRecoveryId(sig[0].toByteArray(), sig[1].toByteArray(), data, publicKey);
for (BigInteger sigChunk : sig) {
sigData.add(sigChunk.toByteArray());
}
sigData.add(new byte[]{recoveryId});
return sigData.toArray(new byte[][]{});
} catch (Exception e) {
LOGGER.error(null, e);
return new byte[0][0];
}
}
示例10: sign
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
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();
}
示例11: signData
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
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.
}
}
示例12: createSignature
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* 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]);
}
示例13: testECDSAKeyGenTest
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
/**
* key generation test
*/
private void testECDSAKeyGenTest()
{
SecureRandom random = new SecureRandom();
ECCurve.Fp curve = new ECCurve.Fp(
new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b
ECDomainParameters params = new ECDomainParameters(
curve,
curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n
ECKeyPairGenerator pGen = new ECKeyPairGenerator();
ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(
params,
random);
pGen.init(genParam);
AsymmetricCipherKeyPair pair = pGen.generateKeyPair();
ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random);
ECDSASigner ecdsa = new ECDSASigner();
ecdsa.init(true, param);
byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").toByteArray();
BigInteger[] sig = ecdsa.generateSignature(message);
ecdsa.init(false, pair.getPublic());
if (!ecdsa.verifySignature(message, sig[0], sig[1]))
{
fail("signature fails");
}
}
示例14: signHash
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
private ECDSASignature signHash(byte[] hashOfBytes) throws Exception {
if(hashOfBytes.length!=32){
throw new RuntimeException("can sign only a hash of 32 bytes");
}
ECDSASigner signer = new ECDSASigner();
ECPrivateKeyParameters privKey = privateKey.getECPrivateKey();
signer.init(true, privKey);
BigInteger[] RandS = signer.generateSignature(hashOfBytes);
return new ECDSASignature(RandS[0], RandS[1], privateKey.getPublicKey().getPublicPoint());
}
示例15: prepareProtocolMessageContents
import org.bouncycastle.crypto.signers.ECDSASigner; //导入方法依赖的package包/类
@Override
protected void prepareProtocolMessageContents() {
message.setTokenbindingType(chooser.getConfig().getDefaultTokenBindingType().getTokenBindingTypeValue());
message.setKeyParameter(chooser.getConfig().getDefaultTokenBindingKeyParameters().get(0).getValue());
if (chooser.getConfig().getDefaultTokenBindingKeyParameters().get(0) == TokenBindingKeyParameters.ECDSAP256) {
ECDomainParameters generateEcParameters = generateEcParameters();
ECPrivateKey tokenBindingECPrivateKey = KeyGenerator.getTokenBindingECPrivateKey(chooser);
LOGGER.debug("Using private Key:" + tokenBindingECPrivateKey.getS());
ECPoint publicKey = generateEcParameters.getG().multiply(tokenBindingECPrivateKey.getS());
publicKey = publicKey.normalize();
CustomECPoint point = new CustomECPoint(publicKey.getRawXCoord().toBigInteger(), publicKey.getRawYCoord()
.toBigInteger());
message.setPoint(ArrayConverter.concatenate(point.getByteX(), point.getByteY()));
message.setPointLength(message.getPoint().getValue().length);
ParametersWithRandom params = new ParametersWithRandom(new ECPrivateKeyParameters(
tokenBindingECPrivateKey.getS(), generateEcParameters), new BadRandom(new Random(0), new byte[0]));
ECDSASigner signer = new ECDSASigner();
signer.init(true, params);
MessageDigest dig = null;
try {
dig = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
dig.update(generateToBeSigned());
BigInteger[] signature = signer.generateSignature(dig.digest());
message.setSignature(ArrayConverter.concatenate(CustomECPoint.toUnsignedByteArray(signature[0]),
CustomECPoint.toUnsignedByteArray(signature[1])));
} else {
message.setModulus(chooser.getConfig().getDefaultTokenBindingRsaModulus().toByteArray());
message.setModulusLength(message.getModulus().getValue().length);
message.setPublicExponent(chooser.getConfig().getDefaultTokenBindingRsaPublicKey().toByteArray());
message.setPublicExponentLength(message.getPublicExponent().getValue().length);
}
TokenBindingMessageSerializer serializer = new TokenBindingMessageSerializer(message,
chooser.getSelectedProtocolVersion());
message.setKeyLength(serializer.serializeKey().length);
message.setExtensionBytes(new byte[0]);
message.setExtensionLength(message.getExtensionBytes().getValue().length);
SignatureAndHashAlgorithm algorithm = new SignatureAndHashAlgorithm(SignatureAlgorithm.ECDSA,
HashAlgorithm.SHA1);
// message.setSignature(SignatureCalculator.generateSignature(chooser.getConfig()
// .getDefaultTokenBindingKeyParameters().get(0), chooser,));
message.setSignatureLength(message.getSignature().getValue().length);
serializer = new TokenBindingMessageSerializer(message, ProtocolVersion.TLS12);
message.setTokenbindingsLength(serializer.serializeBinding().length);
}