Search in sources :

Example 61 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project attestation by TokenScript.

the class Ticket method makeTicket.

ASN1Sequence makeTicket() {
    ASN1EncodableVector ticket = new ASN1EncodableVector();
    ticket.add(new DERUTF8String(devconId));
    ticket.add(new ASN1Integer(ticketId));
    ticket.add(new ASN1Integer(ticketClass));
    ticket.add(new DEROctetString(commitment));
    return new DERSequence(ticket);
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 62 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project attestation by TokenScript.

the class Attestation method setSmartcontracts.

// TODO change to list of arrays of 20 bytes
public void setSmartcontracts(List<Long> smartcontracts) {
    ASN1EncodableVector seq = new ASN1EncodableVector();
    for (long current : smartcontracts) {
        seq.add(new ASN1Integer(current));
    }
    this.smartcontracts = new DERSequence(seq);
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 63 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project attestation by TokenScript.

the class AttestationRequest method getDerEncoding.

@Override
public byte[] getDerEncoding() {
    try {
        ASN1EncodableVector res = new ASN1EncodableVector();
        res.add(new ASN1Integer(type.ordinal()));
        res.add(ASN1Primitive.fromByteArray(pok.getDerEncoding()));
        return new DERSequence(res).getEncoded();
    } catch (IOException e) {
        throw ExceptionUtil.makeRuntimeException(logger, "Could not encode asn1", e);
    }
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException)

Example 64 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project PCNGateway-Java-SDK by BSNDA.

the class EcdsaSign method sign.

public static String sign(String algorithm, String stringPrivateKey, String signString) throws Exception {
    CryptoPrimitives cryptoPrimitives = new CryptoPrimitives();
    cryptoPrimitives.init();
    PrivateKey privateKey = cryptoPrimitives.bytesToPrivateKey(stringPrivateKey.getBytes());
    ECPrivateKey eck = (ECPrivateKey) privateKey;
    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(algorithm);
    ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN());
    ECDSASigner signer = new ECDSASigner();
    ECPrivateKeyParameters parameters = new ECPrivateKeyParameters(eck.getS(), domain);
    signer.init(true, parameters);
    byte[] hash = hash(signString.getBytes());
    BigInteger[] signature = signer.generateSignature(hash);
    BigInteger r = signature[0];
    BigInteger s = signature[1];
    // BIP62: "S must be less than or equal to half of the Group Order N"
    BigInteger overTwo = domain.getN().divide(new BigInteger("2"));
    if (s.compareTo(overTwo) == 1) {
        s = domain.getN().subtract(s);
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DERSequenceGenerator seq = new DERSequenceGenerator(bos);
    seq.addObject(new ASN1Integer(r));
    seq.addObject(new ASN1Integer(s));
    seq.close();
    return java.util.Base64.getEncoder().encodeToString(bos.toByteArray());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECNamedCurveParameterSpec(org.bouncycastle.jce.spec.ECNamedCurveParameterSpec) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator)

Example 65 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project PCNGateway-Java-SDK by BSNDA.

the class Sm2SignUtil method xuperSignature.

public static byte[] xuperSignature(String privateKeyStr, byte[] message) throws Exception {
    PrivateKey privateKey = Sm2SignUtil.getPrivateKey(privateKeyStr.getBytes());
    BCECPrivateKey ecPrivateKey = (BCECPrivateKey) privateKey;
    X9ECParameters sm2ECParameters = GMNamedCurves.getByName("sm2p256v1");
    ECDomainParameters ec = new ECDomainParameters(sm2ECParameters.getCurve(), sm2ECParameters.getG(), sm2ECParameters.getN());
    // Order n
    BigInteger n = ec.getN();
    // Base point G
    ECPoint G = ec.getG();
    BigInteger r, s;
    // Get private key d
    BigInteger d = ecPrivateKey.getD();
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    DSAKCalculator kCalculator = new RandomDSAKCalculator();
    // Initialize the random number generator
    // if (kCalculator.isDeterministic()) {
    // kCalculator.init(n, d, message);
    // } else {
    kCalculator.init(n, new SecureRandom());
    do {
        // Calculate s
        BigInteger k;
        BigInteger e;
        BigInteger tmp;
        BigInteger tmp2;
        do {
            // Calculate r,refers to GM/T 0003.2-2012 6.1
            k = kCalculator.nextK();
            ECPoint p = basePointMultiplier.multiply(G, k).normalize();
            e = org.bouncycastle.util.BigIntegers.fromUnsignedByteArray(message);
            // r = (e + x) mod n
            r = p.getAffineXCoord().toBigInteger().add(e).mod(n);
        } while (r.equals(ZERO) || r.add(k).equals(n));
        // tmp = (1+d).inverse
        tmp = d.add(ONE).modInverse(n);
        // tmp2 = k - r*d
        tmp2 = k.subtract(r.multiply(d));
        s = tmp.multiply(tmp2).mod(n);
    } while (s.equals(ZERO));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DERSequenceGenerator seq = new DERSequenceGenerator(bos);
    seq.addObject(new ASN1Integer(r));
    seq.addObject(new ASN1Integer(s));
    seq.close();
    return bos.toByteArray();
}
Also used : BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ECPoint(org.bouncycastle.math.ec.ECPoint) RandomDSAKCalculator(org.bouncycastle.crypto.signers.RandomDSAKCalculator) DSAKCalculator(org.bouncycastle.crypto.signers.DSAKCalculator) RandomDSAKCalculator(org.bouncycastle.crypto.signers.RandomDSAKCalculator) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator)

Aggregations

ASN1Integer (org.bouncycastle.asn1.ASN1Integer)213 ASN1Integer (com.unboundid.asn1.ASN1Integer)96 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)94 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)91 IOException (java.io.IOException)89 DERSequence (org.bouncycastle.asn1.DERSequence)89 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)86 BigInteger (java.math.BigInteger)86 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)80 ASN1Element (com.unboundid.asn1.ASN1Element)69 Test (org.testng.annotations.Test)63 ArrayList (java.util.ArrayList)50 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)49 DERSequence (com.github.zhenwei.core.asn1.DERSequence)47 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)47 DEROctetString (org.bouncycastle.asn1.DEROctetString)38 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)35 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)28 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)27 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)27