-
Notifications
You must be signed in to change notification settings - Fork 4
/
KMMEdPrivateKey.kt
64 lines (55 loc) · 1.8 KB
/
KMMEdPrivateKey.kt
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
package org.hyperledger.identus.apollo.utils
import node.buffer.Buffer
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import org.hyperledger.identus.apollo.utils.external.eddsa
/**
* Represents a private key in the KMMEd cryptographic system.
*
* @property raw The raw value of the private key.
* @property keyPair The key pair object associated with the private key.
* @constructor Creates a KMMEdPrivateKey object.
* @param bytes The byte array representing the private key.
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
actual class KMMEdPrivateKey(bytes: ByteArray) {
val raw: Buffer
private val keyPair: eddsa.KeyPair
init {
val ed25519 = eddsa("ed25519")
raw = Curve25519Parser.parseRaw(bytes)
keyPair = ed25519.keyFromSecret(raw)
}
/**
* Base64 url encodes the raw value
* @return Buffer
*/
fun getEncoded(): Buffer {
return Buffer.from(raw.toByteArray().base64UrlEncoded)
}
/**
* PublicKey associated with this PrivateKey
* @return KMMEdPublicKey
*/
fun publicKey(): KMMEdPublicKey {
return KMMEdPublicKey(keyPair.getPublic())
}
/**
* Cryptographically sign a given [message]
* @param message - the ByteArray to be signed
* @return ByteArray - signature Hex converted to ByteArray
*/
actual fun sign(message: ByteArray): ByteArray {
val sig = keyPair.sign(Buffer.from(message))
return sig.toBytes().toByteArray()
}
/**
* Method convert an ed25519 private key to a x25519 private key
*
* @return KMMX25519PrivateKey private key
*/
actual fun x25519PrivateKey(): KMMX25519PrivateKey {
val rawX25519Prv = convertSecretKeyToX25519(this.raw.toByteArray())
return KMMX25519PrivateKey(rawX25519Prv)
}
}