forked from DeepCoin/nxt-decompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nxt$Crypto.java
76 lines (63 loc) · 2.06 KB
/
Nxt$Crypto.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Arrays;
class Nxt$Crypto
{
static byte[] getPublicKey(String secretPhrase)
{
try
{
byte[] publicKey = new byte[32];
Nxt.Curve25519.keygen(publicKey, null, Nxt.getMessageDigest("SHA-256").digest(secretPhrase.getBytes("UTF-8")));
return publicKey;
}
catch (RuntimeException|UnsupportedEncodingException e)
{
Nxt.logMessage("Error getting public key", e);
}
return null;
}
static byte[] sign(byte[] message, String secretPhrase)
{
try
{
byte[] P = new byte[32];
byte[] s = new byte[32];
MessageDigest digest = Nxt.getMessageDigest("SHA-256");
Nxt.Curve25519.keygen(P, s, digest.digest(secretPhrase.getBytes("UTF-8")));
byte[] m = digest.digest(message);
digest.update(m);
byte[] x = digest.digest(s);
byte[] Y = new byte[32];
Nxt.Curve25519.keygen(Y, null, x);
digest.update(m);
byte[] h = digest.digest(Y);
byte[] v = new byte[32];
Nxt.Curve25519.sign(v, h, x, s);
byte[] signature = new byte[64];
System.arraycopy(v, 0, signature, 0, 32);
System.arraycopy(h, 0, signature, 32, 32);
return signature;
}
catch (RuntimeException|UnsupportedEncodingException e)
{
Nxt.logMessage("Error in signing message", e);
}
return null;
}
static boolean verify(byte[] signature, byte[] message, byte[] publicKey)
{
try
{
byte[] Y = new byte[32];
byte[] v = new byte[32];
System.arraycopy(signature, 0, v, 0, 32);
byte[] h = new byte[32];
System.arraycopy(signature, 32, h, 0, 32);
Nxt.Curve25519.verify(Y, v, h, publicKey);
MessageDigest digest = Nxt.getMessageDigest("SHA-256");
byte[] m = digest.digest(message);
digest.update(m);
byte[] h2 = digest.digest(Y);
return Arrays.equals(h, h2);
}