public byte[] sign() throws Exception{ byte[] sig=signature.sign(); // It seems that the output from SunEC is in ASN.1, // so we have to convert it. if(sig[0]==0x30 && // in ASN.1 ((sig[1]+2 == sig.length) || ((sig[1]&0x80)!=0 && (sig[2]&0xff)+3==sig.length))){// 2bytes for len int index=3; if((sig[1]&0x80)!=0 && (sig[2]&0xff)+3==sig.length) index=4; byte[] r = new byte[sig[index]]; byte[] s = new byte[sig[index+2+sig[index]]]; System.arraycopy(sig, index+1, r, 0, r.length); System.arraycopy(sig, index+3+sig[index], s, 0, s.length); r = chop0(r); s = chop0(s); Buffer buf = new Buffer(); buf.putMPInt(r); buf.putMPInt(s); sig=new byte[buf.getLength()]; buf.setOffSet(0); buf.getByte(sig); } return sig; }
public boolean verify(byte[] sig) throws Exception{ // It seems that SunEC expects ASN.1 data, // so we have to convert it. if(!(sig[0]==0x30 && // not in ASN.1 ((sig[1]+2 == sig.length) || ((sig[1]&0x80)!=0 && (sig[2]&0xff)+3==sig.length)))) { Buffer b = new Buffer(sig); b.getString(); // ecdsa-sha2-nistp256 b.getInt(); byte[] r = b.getMPInt(); byte[] s = b.getMPInt(); r=insert0(r); s=insert0(s); byte[] asn1 = null; if(r.length<64){ asn1 = new byte[6+r.length+s.length]; asn1[0] = (byte)0x30; asn1[1] = (byte)(4+r.length+s.length); asn1[2] = (byte)0x02; asn1[3] = (byte)r.length; System.arraycopy(r, 0, asn1, 4, r.length); asn1[r.length+4] = (byte)0x02; asn1[r.length+5] = (byte)s.length; System.arraycopy(s, 0, asn1, (6+r.length), s.length); } else { asn1 = new byte[6+r.length+s.length+1]; asn1[0] = (byte)0x30; asn1[1] = (byte)0x81; asn1[2] = (byte)(4+r.length+s.length); asn1[3] = (byte)0x02; asn1[4] = (byte)r.length; System.arraycopy(r, 0, asn1, 5, r.length); asn1[r.length+5] = (byte)0x02; asn1[r.length+6] = (byte)s.length; System.arraycopy(s, 0, asn1, (7+r.length), s.length); } sig=asn1; } return signature.verify(sig); }