Example usage for org.bouncycastle.asn1.x9 X9IntegerConverter X9IntegerConverter

List of usage examples for org.bouncycastle.asn1.x9 X9IntegerConverter X9IntegerConverter

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x9 X9IntegerConverter X9IntegerConverter.

Prototype

X9IntegerConverter

Source Link

Usage

From source file:at.asitplus.regkassen.common.util.CryptoUtil.java

License:Apache License

/**
 * Helper method to convert DER-encoded signature values (e.g. used by Java)
 * to concatenated signature values//  w  w  w .  j a v a 2 s . co  m
 * (as used by the JWS-standard)
 *
 * @param derEncodedSignatureValue
 *          DER-encoded signature value
 * @return concatenated signature value (as used by JWS standard)
 * @throws IOException
 */
public static byte[] convertDEREncodedSignatureToJWSConcatenated(final byte[] derEncodedSignatureValue)
        throws IOException {
    final ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue);
    final ASN1Primitive asn1Primitive = asn1InputStream.readObject();
    asn1InputStream.close();
    final ASN1Sequence asn1Sequence = (ASN1Sequence.getInstance(asn1Primitive));
    final ASN1Integer rASN1 = (ASN1Integer) asn1Sequence.getObjectAt(0);
    final ASN1Integer sASN1 = (ASN1Integer) asn1Sequence.getObjectAt(1);
    final X9IntegerConverter x9IntegerConverter = new X9IntegerConverter();
    final byte[] r = x9IntegerConverter.integerToBytes(rASN1.getValue(), 32);
    final byte[] s = x9IntegerConverter.integerToBytes(sASN1.getValue(), 32);

    final byte[] concatenatedSignatureValue = new byte[64];
    System.arraycopy(r, 0, concatenatedSignatureValue, 0, 32);
    System.arraycopy(s, 0, concatenatedSignatureValue, 32, 32);

    return concatenatedSignatureValue;
}

From source file:at.asitplus.regkassen.core.base.util.CryptoUtil.java

License:Apache License

/**
 * Helper method to convert DER-encoded signature values (e.g. used by Java) to concatenated signature values
 * (as used by the JWS-standard)// ww w  . j  a  va  2  s .  com
 *
 * @param derEncodedSignatureValue DER-encoded signature value
 * @return concatenated signature value (as used by JWS standard)
 * @throws IOException
 */
public static byte[] convertDEREncodedSignatureToJWSConcatenated(byte[] derEncodedSignatureValue)
        throws IOException {
    ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue);
    ASN1Primitive asn1Primitive = asn1InputStream.readObject();
    ASN1Sequence asn1Sequence = (ASN1Sequence.getInstance(asn1Primitive));
    ASN1Integer rASN1 = (ASN1Integer) asn1Sequence.getObjectAt(0);
    ASN1Integer sASN1 = (ASN1Integer) asn1Sequence.getObjectAt(1);
    X9IntegerConverter x9IntegerConverter = new X9IntegerConverter();
    byte[] r = x9IntegerConverter.integerToBytes(rASN1.getValue(), 32);
    byte[] s = x9IntegerConverter.integerToBytes(sASN1.getValue(), 32);

    byte[] concatenatedSignatureValue = new byte[64];
    System.arraycopy(r, 0, concatenatedSignatureValue, 0, 32);
    System.arraycopy(s, 0, concatenatedSignatureValue, 32, 32);

    return concatenatedSignatureValue;
}

From source file:com.google.bitcoin.core.ECKey.java

License:Apache License

/** Decompress a compressed public key (x co-ord and low-bit of y-coord). */
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve()));
    compEnc[0] = (byte) (yBit ? 0x03 : 0x02);
    return CURVE.getCurve().decodePoint(compEnc);
}