@Test public void testSign() throws Exception { final String dataHex = "010100000100009803555e0020000000f5496c59ff336ae2d497140f0ad48306092beeda0d36c31c7373103956c9026180841e000000000063a95e002800000054435550565143373754414d4837514b504650354f5433544c5556344a59525056364345474a585700ca9a3b000000001900000001000000110000004669727374205445535421205965616821"; final byte[] data = HexEncoder.getBytes(dataHex); final String privateKeyHex = "c7a10487f0c2be5cf691b42864e13be95d172f67aa1c8e018932ce09f700d962"; final String publicKeyHex = "f5496c59ff336ae2d497140f0ad48306092beeda0d36c31c7373103956c90261"; final KeyPair keyPair = new KeyPair(PrivateKey.fromHexString(privateKeyHex), PublicKey.fromHexString(publicKeyHex)); DsaSigner dsaSigner = new Ed25519DsaSigner(keyPair); final byte[] expectedSignature = HexEncoder.getBytes("79eb96112c2c57d064316105b80ebe6c14eb2622b25844b96d96ed51136cb434f1919658f02eaf8c592ac5e55d38bce4a64fcadeece160295dd16fb2955f020a"); final byte[] actual = dsaSigner.sign(data).getBytes(); Assert.assertArrayEquals("Wrong signature!", expectedSignature, actual); }
@Override public Signature sign(final byte[] data) { if (!this.getKeyPair().hasPrivateKey()) { throw new CryptoException("cannot sign without private key"); } final byte[] hash = Hashes.sha3_512(ArrayUtils.toByteArray(this.getKeyPair().getPrivateKey().getRaw(), 32)); final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement( Hashes.sha3_512(Arrays.copyOfRange(hash, 32, 64), data)); final Ed25519EncodedFieldElement rModQ = r.modQ(); final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ); final Ed25519EncodedGroupElement encodedR = R.encode(); final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(Hashes.sha3_512( encodedR.getRaw(), this.getKeyPair().getPublicKey().getRaw(), data)); final Ed25519EncodedFieldElement hModQ = h.modQ(); final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ( Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ); final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw()); if (!this.isCanonicalSignature(signature)) { throw new CryptoException("Generated signature is not canonical"); } return signature; }
Ed25519DsaSigner implements DsaSigner { @Override public Signature sign(final byte[] data) { if (!this.getKeyPair().hasPrivateKey()) { throw new CryptoException("cannot sign without private key"); } final byte[] hash = Hashes.sha3_512(ArrayUtils.toByteArray(this.getKeyPair().getPrivateKey().getRaw(), 32)); final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement( Hashes.sha3_512(Arrays.copyOfRange(hash, 32, 64), data)); final Ed25519EncodedFieldElement rModQ = r.modQ(); final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ); final Ed25519EncodedGroupElement encodedR = R.encode(); final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(Hashes.sha3_512( encodedR.getRaw(), this.getKeyPair().getPublicKey().getRaw(), data)); final Ed25519EncodedFieldElement hModQ = h.modQ(); final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ( Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ); final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw()); if (!this.isCanonicalSignature(signature)) { throw new CryptoException("Generated signature is not canonical"); } return signature; } }
Ed25519DsaSigner implements DsaSigner { @Override public Signature sign(final byte[] data) { if (!this.getKeyPair().hasPrivateKey()) { throw new CryptoException("cannot sign without private key"); } final byte[] hash = Hashes.sha3_512(ArrayUtils.toByteArray(this.getKeyPair().getPrivateKey().getRaw(), 32)); final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement( Hashes.sha3_512(Arrays.copyOfRange(hash, 32, 64), data)); final Ed25519EncodedFieldElement rModQ = r.modQ(); final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ); final Ed25519EncodedGroupElement encodedR = R.encode(); final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(Hashes.sha3_512( encodedR.getRaw(), this.getKeyPair().getPublicKey().getRaw(), data)); final Ed25519EncodedFieldElement hModQ = h.modQ(); final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ( Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ); final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw()); if (!this.isCanonicalSignature(signature)) { throw new CryptoException("Generated signature is not canonical"); } return signature; } Ed25519DsaSigner(final KeyPair keyPair); }
Ed25519DsaSigner implements DsaSigner { @Override public Signature sign(final byte[] data) { if (!this.getKeyPair().hasPrivateKey()) { throw new CryptoException("cannot sign without private key"); } final byte[] hash = Hashes.sha3_512(ArrayUtils.toByteArray(this.getKeyPair().getPrivateKey().getRaw(), 32)); final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement( Hashes.sha3_512(Arrays.copyOfRange(hash, 32, 64), data)); final Ed25519EncodedFieldElement rModQ = r.modQ(); final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ); final Ed25519EncodedGroupElement encodedR = R.encode(); final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(Hashes.sha3_512( encodedR.getRaw(), this.getKeyPair().getPublicKey().getRaw(), data)); final Ed25519EncodedFieldElement hModQ = h.modQ(); final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ( Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ); final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw()); if (!this.isCanonicalSignature(signature)) { throw new CryptoException("Generated signature is not canonical"); } return signature; } Ed25519DsaSigner(final KeyPair keyPair); KeyPair getKeyPair(); @Override Signature sign(final byte[] data); @Override boolean verify(final byte[] data, final Signature signature); @Override boolean isCanonicalSignature(final Signature signature); @Override Signature makeSignatureCanonical(final Signature signature); }
Ed25519DsaSigner implements DsaSigner { @Override public Signature sign(final byte[] data) { if (!this.getKeyPair().hasPrivateKey()) { throw new CryptoException("cannot sign without private key"); } final byte[] hash = Hashes.sha3_512(ArrayUtils.toByteArray(this.getKeyPair().getPrivateKey().getRaw(), 32)); final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement( Hashes.sha3_512(Arrays.copyOfRange(hash, 32, 64), data)); final Ed25519EncodedFieldElement rModQ = r.modQ(); final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ); final Ed25519EncodedGroupElement encodedR = R.encode(); final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(Hashes.sha3_512( encodedR.getRaw(), this.getKeyPair().getPublicKey().getRaw(), data)); final Ed25519EncodedFieldElement hModQ = h.modQ(); final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ( Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ); final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw()); if (!this.isCanonicalSignature(signature)) { throw new CryptoException("Generated signature is not canonical"); } return signature; } Ed25519DsaSigner(final KeyPair keyPair); KeyPair getKeyPair(); @Override Signature sign(final byte[] data); @Override boolean verify(final byte[] data, final Signature signature); @Override boolean isCanonicalSignature(final Signature signature); @Override Signature makeSignatureCanonical(final Signature signature);  }