private static BigInteger [ ] decodeECDSASignature ( byte [ ] signature ) throws Exception { try ( ByteArrayInputStream inStream = new ByteArrayInputStream ( signature ) ) { ASN1InputStream asnInputStream = new ASN1InputStream ( inStream ) ; ASN1Primitive asn1 = asnInputStream . readObject ( ) ; BigInteger [ ] sigs = new BigInteger [ 2 ] ; int count = 0 ; if ( asn1 instanceof ASN1Sequence ) { ASN1Sequence asn1Sequence = ( ASN1Sequence ) asn1 ; ASN1Encodable [ ] asn1Encodables = asn1Sequence . toArray ( ) ; for ( ASN1Encodable asn1Encodable : asn1Encodables ) { ASN1Primitive asn1Primitive = asn1Encodable . toASN1Primitive ( ) ; if ( asn1Primitive instanceof ASN1Integer ) { ASN1Integer asn1Integer = ( ASN1Integer ) asn1Primitive ; BigInteger integer = asn1Integer . getValue ( ) ; if ( count < 2 ) { sigs [ count ] = integer ; } count ++ ; } } } if ( count != 2 ) { throw new CryptoException ( format ( "Invalid ECDSA signature. Expected count of 2 but got: %d. Signature is: %s" , count , DatatypeConverter . printHexBinary ( signature ) ) ) ; } return sigs ; } }
Decodes an ECDSA signature and returns a two element BigInteger array .