/** * Sign data and return a fixed size signature. The data does not need to be hashed, the * signing code will handle that for us, using an algorithm appropriate for the keysize. * @return A zero padded DER signature (maxSigSize). Space Inefficient but constant-size. */ public byte[] signToNetworkFormat(byte[]... data) { byte[] plainsig = sign(data); int targetLength = curve.maxSigSize; if(plainsig.length != targetLength) { byte[] newData = new byte[targetLength]; if(plainsig.length < targetLength) { System.arraycopy(plainsig, 0, newData, 0, plainsig.length); } else { throw new IllegalStateException("Too long!"); } plainsig = newData; } return plainsig; }
int max = 0; for(int i=0; i<10000; i++) { max = Math.max(max, ecdsa.sign(signedBytes).length);
private String ecdsaSignRef(String mySignedReference) throws NodeInitException { if(logMINOR) Logger.minor(this, "Signing reference:\n"+mySignedReference); try{ byte[] ref = mySignedReference.getBytes("UTF-8"); // We don't need a padded signature here byte[] sig = ecdsaP256.sign(ref); if(logMINOR && !ECDSA.verify(Curves.P256, getECDSAP256Pubkey(), sig, ref)) throw new NodeInitException(NodeInitException.EXIT_EXCEPTION_TO_DEBUG, mySignedReference); return Base64.encode(sig); } catch(UnsupportedEncodingException e){ //duh ? Logger.error(this, "Error while signing the node identity!" + e, e); System.err.println("Error while signing the node identity!"+e); e.printStackTrace(); throw new NodeInitException(NodeInitException.EXIT_CRAPPY_JVM, "Impossible: JVM doesn't support UTF-8"); } }
public void testSign() { byte[] sig= ecdsa.sign("test".getBytes()); assertNotNull(sig); assertTrue(sig.length > 0); }
public void testVerify() { String toSign = "test"; byte[] sig= ecdsa.sign(toSign.getBytes()); assertTrue(ecdsa.verify(sig, toSign.getBytes())); assertFalse(ecdsa.verify(sig, "".getBytes())); }