Skip to content

Commit 0db77f3

Browse files
committed
Add Signature.detached() and Signature.detached_verify() methods.
1 parent 6382765 commit 0db77f3

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/com/iwebpp/crypto/TweetNacl.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,11 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
695695
* Signs the message using the secret key and returns a signature.
696696
* */
697697
public byte [] detached(byte [] message) {
698-
699-
return null;
698+
byte[] signedMsg = this.sign(message);
699+
byte[] sig = new byte[signatureLength];
700+
for (int i = 0; i < sig.length; i++)
701+
sig[i] = signedMsg[i];
702+
return sig;
700703
}
701704

702705
/*
@@ -705,8 +708,17 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
705708
* returns true if verification succeeded or false if it failed.
706709
* */
707710
public boolean detached_verify(byte [] message, byte [] signature) {
708-
709-
return false;
711+
if (signature.length != signatureLength)
712+
return false;
713+
if (theirPublicKey.length != publicKeyLength)
714+
return false;
715+
byte [] sm = new byte[signatureLength + message.length];
716+
byte [] m = new byte[signatureLength + message.length];
717+
for (int i = 0; i < signatureLength; i++)
718+
sm[i] = signature[i];
719+
for (int i = 0; i < message.length; i++)
720+
sm[i + signatureLength] = message[i];
721+
return (crypto_sign_open(m, -1, sm, sm.length, theirPublicKey) >= 0);
710722
}
711723

712724
/*

src/com/iwebpp/crypto/TweetNaclFast.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,11 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
762762
* Signs the message using the secret key and returns a signature.
763763
* */
764764
public byte [] detached(byte [] message) {
765-
766-
return null;
765+
byte[] signedMsg = this.sign(message);
766+
byte[] sig = new byte[signatureLength];
767+
for (int i = 0; i < sig.length; i++)
768+
sig[i] = signedMsg[i];
769+
return sig;
767770
}
768771

769772
/*
@@ -772,8 +775,17 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
772775
* returns true if verification succeeded or false if it failed.
773776
* */
774777
public boolean detached_verify(byte [] message, byte [] signature) {
775-
776-
return false;
778+
if (signature.length != signatureLength)
779+
return false;
780+
if (theirPublicKey.length != publicKeyLength)
781+
return false;
782+
byte [] sm = new byte[signatureLength + message.length];
783+
byte [] m = new byte[signatureLength + message.length];
784+
for (int i = 0; i < signatureLength; i++)
785+
sm[i] = signature[i];
786+
for (int i = 0; i < message.length; i++)
787+
sm[i + signatureLength] = message[i];
788+
return (crypto_sign_open(m, -1, sm, 0, sm.length, theirPublicKey) >= 0);
777789
}
778790

779791
/*

0 commit comments

Comments
 (0)