package qora crypto import Crypto sign verify functions defined in the

 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
package qora.crypto
import Crypto._
/*
sign & verify functions defined in the same way as in Nxt
*/
object Curve25519Impl {
def createKeyPair(seed: Array[Byte]): (Array[Byte], Array[Byte]) = {
val privateKey = new Array[Byte](32)
val publicKey = new Array[Byte](32)
Curve25519.keygen(publicKey, privateKey, seed)
privateKey -> publicKey
}
def sign(privateKey: Array[Byte], publicKey: Array[Byte], message: Array[Byte]): Array[Byte] = {
require(privateKey.length == 32)
require(publicKey.length == 32)
val m = sha256(message)
val x = sha256(m ++ privateKey)
val Y = new Array[Byte](32)
Curve25519.keygen(Y, null, x)
val h = sha256(m ++ Y)
val v = new Array[Byte](32)
Curve25519.sign(v, h, x, privateKey)
v ++ h
}
def verify(signature: Array[Byte], message: Array[Byte], publicKey: Array[Byte]): Boolean = {
require(signature.length == 64)
require(publicKey.length == 32)
val v = new Array[Byte](32)
val h = new Array[Byte](32)
System.arraycopy(signature, 0, v, 0, 32)
System.arraycopy(signature, 32, h, 0, 32)
val Y = new Array[Byte](32)
Curve25519.verify(Y, v, h, publicKey)
val m = sha256(message)
val h2 = sha256(m ++ Y)
h.sameElements(h2)
}
}