List of usage examples for org.bouncycastle.asn1 DERSequenceGenerator addObject
public void addObject(ASN1Encodable object) throws IOException
From source file:at.asitplus.regkassen.common.util.CryptoUtil.java
License:Apache License
/** * Helper method to convert concatenated signature values (as used by the JWS-standard) to * DER-encoded signature values (e.g. used by Java) * * @param concatenatedSignatureValue/*from w ww . j ava2 s . com*/ * concatenated signature value (as used by JWS standard) * @return DER-encoded signature value * @throws IOException */ public static byte[] convertJWSConcatenatedToDEREncodedSignature(final byte[] concatenatedSignatureValue) throws IOException { final byte[] r = new byte[33]; final byte[] s = new byte[33]; System.arraycopy(concatenatedSignatureValue, 0, r, 1, 32); System.arraycopy(concatenatedSignatureValue, 32, s, 1, 32); final BigInteger rBigInteger = new BigInteger(r); final BigInteger sBigInteger = new BigInteger(s); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final DERSequenceGenerator seqGen = new DERSequenceGenerator(bos); seqGen.addObject(new ASN1Integer(rBigInteger.toByteArray())); seqGen.addObject(new ASN1Integer(sBigInteger.toByteArray())); seqGen.close(); bos.close(); final byte[] derEncodedSignatureValue = bos.toByteArray(); return derEncodedSignatureValue; }
From source file:ca.trustpoint.m2m.EntityName.java
License:Apache License
/** * Returns the DER encoding of this instance. * * @return The DER encoding of this instance. * @throws IOException if this instance cannot be encoded. *///w w w.j a v a2 s.co m public byte[] getEncoded() throws IOException { if (!isValid()) { throw new IOException("EntityName is not valid."); } ByteArrayOutputStream encodedBytes = new ByteArrayOutputStream(); DERSequenceGenerator generator = new DERSequenceGenerator(encodedBytes); for (EntityNameAttribute attribute : attributes) { generator.addObject(DERTaggedObject.getInstance(attribute.getEncoded())); } generator.close(); encodedBytes.close(); return (encodedBytes.toByteArray()); }
From source file:com.bitsofproof.supernode.api.ECKeyPair.java
License:Apache License
@Override public byte[] sign(byte[] hash) throws ValidationException { if (priv == null) { throw new ValidationException("Need private key to sign"); }//from w ww. j a va2 s .co m ECDSASigner signer = new ECDSASigner(); 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 DERInteger(signature[0])); seq.addObject(new DERInteger(signature[1])); seq.close(); return s.toByteArray(); } catch (IOException e) { } return null; }
From source file:com.facebook.delegatedrecovery.RecoveryToken.java
License:Open Source License
private byte[] getSignature(final byte[] rawArray, final ECPrivateKey privateKey) throws IOException { if (this.signature != null) { throw new IllegalStateException("This token already has a signature."); }// w w w. j a v a2 s.com final BigInteger privatePoint = privateKey.getS(); final SHA256Digest digest = new SHA256Digest(); final byte[] hash = new byte[digest.getByteLength()]; digest.update(rawArray, 0, rawArray.length); digest.doFinal(hash, 0); final ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(privatePoint, DelegatedRecoveryUtils.P256_DOMAIN_PARAMS)); final BigInteger[] signature = signer.generateSignature(hash); final ByteArrayOutputStream s = new ByteArrayOutputStream(); final DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(signature[1])); seq.close(); return s.toByteArray(); }
From source file:com.google.bitcoin.core.ECKey.java
License:Apache License
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference * implementation in its wallet storage format. *///from w w w . j a va 2 s .com public byte[] toASN1() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(priv.toByteArray())); seq.addObject(new DERTaggedObject(0, SECNamedCurves.getByName("secp256k1").toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
From source file:it.nibbles.javacoin.keyfactory.ecc.KeyImpl.java
License:Open Source License
/** * Sign a block of data with this private key. * @param data The data to sign.//from ww w.j av a2 s.com * @return The signature of the data compatible with Bitcoin specification. */ @Override public byte[] sign(byte[] data) throws VerificationException { // Bitcoin wiki specifies signature to be produced by EC-DSA, so init ECDSASigner signer = new ECDSASigner(); signer.init(true, new ECPrivateKeyParameters(privateKey, domainParameters)); // Sign BigInteger[] signature = signer.generateSignature(data); // As per specification signatures must be DER encoded, and both components // must be concatenated. Luckily bouncycastle provides that also. try { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); DERSequenceGenerator derOutput = new DERSequenceGenerator(byteOutput); derOutput.addObject(new ASN1Integer(signature[0])); derOutput.addObject(new ASN1Integer(signature[1])); derOutput.close(); return byteOutput.toByteArray(); } catch (IOException e) { throw new VerificationException("could not encode signature to DER stream", e); } }
From source file:org.ebayopensource.fido.uaf.crypto.Asn1.java
License:Apache License
/** * DER - From Big Integer rs to byte[]// ww w. j a v a2s. c o m * UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06 DER [ITU-X690-2008] encoded * ECDSA signature [RFC5480] on the secp256k1 curve. I.e. a DER encoded * SEQUENCE { r INTEGER, s INTEGER } * * @param signature * @return * @throws IOException */ public static byte[] getEncoded(BigInteger[] sigs) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(sigs[0])); seq.addObject(new ASN1Integer(sigs[1])); seq.close(); return bos.toByteArray(); }
From source file:org.hyperledger.common.BouncyCastleCrypto.java
License:Apache License
@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 {// ww w .ja v a 2 s . co m 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]; } }
From source file:org.hyperledger.common.PrivateKey.java
License:Apache License
/** * Sign a digest with this key./* w w w. j a va2 s .c o m*/ * * @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; }
From source file:org.hyperledger.fabric.sdk.helper.ChainUtils.java
License:Open Source License
/** * used asn1 and get hash/*from ww w. ja v a 2 s.com*/ * @param blockNumber * @param previousHash * @param dataHash * @return byte[] * @throws IOException * @throws InvalidArgumentException */ public static byte[] calculateBlockHash(long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException { if (previousHash == null) { throw new InvalidArgumentException("previousHash parameter is null."); } if (dataHash == null) { throw new InvalidArgumentException("dataHash parameter is null."); } if (null == suite) { suite = CryptoSuite.Factory.getCryptoSuite(); } ByteArrayOutputStream s = new ByteArrayOutputStream(); DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new ASN1Integer(blockNumber)); seq.addObject(new DEROctetString(previousHash)); seq.addObject(new DEROctetString(dataHash)); seq.close(); return suite.hash(s.toByteArray()); }