本文整理汇总了Java中org.bouncycastle.asn1.DERSequenceGenerator类的典型用法代码示例。如果您正苦于以下问题:Java DERSequenceGenerator类的具体用法?Java DERSequenceGenerator怎么用?Java DERSequenceGenerator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DERSequenceGenerator类属于org.bouncycastle.asn1包,在下文中一共展示了DERSequenceGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sign
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的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: encodeToDER
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
/**
* Encodes R and S as a DER-encoded byte stream
*
* @return DER-encoded byte stream
*/
public byte[] encodeToDER() {
byte[] encodedBytes = null;
try {
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream(80)) {
DERSequenceGenerator seq = new DERSequenceGenerator(outStream);
seq.addObject(new ASN1Integer(r));
seq.addObject(new ASN1Integer(s));
seq.close();
encodedBytes = outStream.toByteArray();
}
} catch (IOException exc) {
throw new IllegalStateException("Unexpected IOException", exc);
}
return encodedBytes;
}
示例3: sign
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的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: open
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
/**
* @deprecated use open(OutputStream, ASN1ObjectIdentifier, ContentCompressor)
*/
public OutputStream open(
OutputStream out,
String contentOID,
String compressionOID)
throws IOException
{
BERSequenceGenerator sGen = new BERSequenceGenerator(out);
sGen.addObject(CMSObjectIdentifiers.compressedData);
//
// Compressed Data
//
BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
cGen.addObject(new ASN1Integer(0));
//
// AlgorithmIdentifier
//
DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());
algGen.addObject(new ASN1ObjectIdentifier(ZLIB));
algGen.close();
//
// Encapsulated ContentInfo
//
BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());
eiGen.addObject(new ASN1ObjectIdentifier(contentOID));
OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
eiGen.getRawOutputStream(), 0, true, _bufferSize);
return new CmsCompressedOutputStream(
new DeflaterOutputStream(octetStream), sGen, cGen, eiGen);
}
示例5: sign
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的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;
}
示例6: ecdsaSignToBytes
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
/**
* Sign data with the specified elliptic curve private key.
*
* @param privateKey elliptic curve private key.
* @param data data to sign
* @return the signed data.
* @throws CryptoException
*/
private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException {
try {
X9ECParameters params = ECNamedCurveTable.getByName(curveName);
BigInteger curveN = params.getN();
Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) :
Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER);
sig.initSign(privateKey);
sig.update(data);
byte[] signature = sig.sign();
BigInteger[] sigs = decodeECDSASignature(signature);
sigs = preventMalleability(sigs, curveN);
ByteArrayOutputStream s = new ByteArrayOutputStream();
DERSequenceGenerator seq = new DERSequenceGenerator(s);
seq.addObject(new ASN1Integer(sigs[0]));
seq.addObject(new ASN1Integer(sigs[1]));
seq.close();
return s.toByteArray();
} catch (Exception e) {
throw new CryptoException("Could not sign the message using private key", e);
}
}
示例7: calculateBlockHash
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
/**
* used asn1 and get hash
*
* @param blockNumber
* @param previousHash
* @param dataHash
* @return byte[]
* @throws IOException
* @throws InvalidArgumentException
*/
public static byte[] calculateBlockHash(HFClient client, 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 == client) {
throw new InvalidArgumentException("client parameter is null.");
}
CryptoSuite cryptoSuite = client.getCryptoSuite();
if (null == client) {
throw new InvalidArgumentException("Client crypto suite has not been set.");
}
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 cryptoSuite.hash(s.toByteArray());
}
示例8: sign
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的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();
}
示例9: signData
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的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.
}
}
示例10: testDERWriting
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
public void testDERWriting()
throws Exception
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DERSequenceGenerator seqGen = new DERSequenceGenerator(bOut);
seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));
seqGen.addObject(new DERObjectIdentifier("1.1"));
seqGen.close();
assertTrue("basic DER writing test failed.", Arrays.equals(seqData, bOut.toByteArray()));
}
示例11: testDERExplicitTaggedSequenceWriting
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
public void testDERExplicitTaggedSequenceWriting()
throws Exception
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DERSequenceGenerator seqGen = new DERSequenceGenerator(bOut, 1, true);
seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));
seqGen.addObject(new DERObjectIdentifier("1.1"));
seqGen.close();
assertTrue("explicit tag writing test failed.", Arrays.equals(expTagSeqData, bOut.toByteArray()));
}
示例12: testDERImplicitTaggedSequenceWriting
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
public void testDERImplicitTaggedSequenceWriting()
throws Exception
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DERSequenceGenerator seqGen = new DERSequenceGenerator(bOut, 1, false);
seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));
seqGen.addObject(new DERObjectIdentifier("1.1"));
seqGen.close();
assertTrue("implicit tag writing test failed.", Arrays.equals(implTagSeqData, bOut.toByteArray()));
}
示例13: encodeToDER
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
/**
* What we get back from the signer are the two components of a signature, r and s. To get a flat byte stream
* of the type used by Bitcoin we have to encode them using DER encoding, which is just a way to pack the two
* components into a structure.
*/
public byte[] encodeToDER() {
try {
// Usually 70-72 bytes.
ByteArrayOutputStream bos = new ByteArrayOutputStream(72);
DERSequenceGenerator seq = new DERSequenceGenerator(bos);
seq.addObject(new DERInteger(r));
seq.addObject(new DERInteger(s));
seq.close();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
示例14: testNestedDERWriting
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
public void testNestedDERWriting()
throws Exception
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DERSequenceGenerator seqGen1 = new DERSequenceGenerator(bOut);
seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));
seqGen1.addObject(new DERObjectIdentifier("1.1"));
DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream());
seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));
seqGen2.close();
seqGen1.close();
assertTrue("nested DER writing test failed.", Arrays.equals(nestedSeqData, bOut.toByteArray()));
}
示例15: testNestedExplicitTagDERWriting
import org.bouncycastle.asn1.DERSequenceGenerator; //导入依赖的package包/类
public void testNestedExplicitTagDERWriting()
throws Exception
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DERSequenceGenerator seqGen1 = new DERSequenceGenerator(bOut);
seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));
seqGen1.addObject(new DERObjectIdentifier("1.1"));
DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream(), 1, true);
seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));
seqGen2.close();
seqGen1.close();
assertTrue("nested explicit tagged DER writing test failed.", Arrays.equals(nestedSeqExpTagData, bOut.toByteArray()));
}