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);
}
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);
}
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);
}
}
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());
}
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();
}
Aggregations