-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
113 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...yption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/Ed25519KeyPairGeneration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
interface Ed25519KeyPairGeneration { | ||
fun generateEd25519KeyPair(): KMMEdKeyPair | ||
fun generateKeyPair(): KMMEdKeyPair | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
...tric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
expect class KMMEdPrivateKey | ||
expect class KMMEdPrivateKey { | ||
fun getEncoded(): ByteArray | ||
|
||
fun sign(message: ByteArray): ByteArray | ||
} |
6 changes: 5 additions & 1 deletion
6
...etric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPublicKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
expect class KMMEdPublicKey | ||
expect class KMMEdPublicKey { | ||
fun getEncoded(): ByteArray | ||
|
||
fun verify(message: ByteArray, sig: ByteArray): Boolean | ||
} |
15 changes: 15 additions & 0 deletions
15
...ic-encryption/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPairTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertNotNull | ||
|
||
class KMMEdKeyPairTests { | ||
@Test | ||
fun testGenerateKeyPair() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
|
||
assertNotNull(keyPair) | ||
assertNotNull(keyPair.privateKey) | ||
assertNotNull(keyPair.publicKey) | ||
} | ||
} |
27 changes: 21 additions & 6 deletions
27
...-asymmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPair.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import io.iohk.atala.prism.apollo.utils.external.rand | ||
import node.buffer.Buffer | ||
|
||
actual class KMMEdKeyPair actual constructor( | ||
actual val privateKey: KMMEdPrivateKey, | ||
actual val publicKey: KMMEdPublicKey | ||
) { | ||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
actual fun sign(message: ByteArray): ByteArray { | ||
return privateKey.sign(message) | ||
} | ||
|
||
actual fun verify(message: ByteArray, sig: ByteArray): Boolean { | ||
return publicKey.verify(message, sig) | ||
} | ||
|
||
actual companion object : Ed25519KeyPairGeneration { | ||
override fun generateEd25519KeyPair(): KMMEdKeyPair { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
override fun generateKeyPair(): KMMEdKeyPair { | ||
val ed25519 = eddsa("ed25519") | ||
val rnd = rand(32) | ||
val secret = Buffer.from(rnd) | ||
val keypair = ed25519.keyFromSecret(secret) | ||
val secretBytes = secret.toByteArray().base64UrlEncoded.encodeToByteArray() | ||
val public = Buffer.from(keypair.getPublic()) | ||
val publicBytes = public.toByteArray().base64UrlEncoded.encodeToByteArray() | ||
|
||
return KMMEdKeyPair(KMMEdPrivateKey(secretBytes), KMMEdPublicKey(publicBytes)) | ||
} | ||
} | ||
} |
27 changes: 24 additions & 3 deletions
27
...ymmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,29 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
actual class KMMEdPrivateKey { | ||
import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes | ||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import node.buffer.Buffer | ||
|
||
actual class KMMEdPrivateKey(val nativeValue: ByteArray) { | ||
private val keyPair: eddsa.KeyPair | ||
|
||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
val ed25519 = eddsa("ed25519") | ||
val decoded = nativeValue.decodeToString().base64UrlDecodedBytes | ||
val secret = Buffer.from(decoded) | ||
|
||
keyPair = ed25519.keyFromSecret(secret) | ||
} | ||
|
||
actual fun getEncoded(): ByteArray { | ||
val secret = Buffer.from(keyPair.getSecret()).toByteArray() | ||
val secretBytes = secret.base64UrlEncoded.encodeToByteArray() | ||
return Buffer.from(secretBytes).toByteArray() | ||
} | ||
|
||
actual fun sign(message: ByteArray): ByteArray { | ||
val sig = keyPair.sign(Buffer.from(message)) | ||
return Buffer.from(sig.toBytes()).toByteArray() | ||
} | ||
} |
25 changes: 22 additions & 3 deletions
25
...symmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPublicKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
actual class KMMEdPublicKey { | ||
import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes | ||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import node.buffer.Buffer | ||
|
||
actual class KMMEdPublicKey(val nativeValue: ByteArray) { | ||
private val keyPair: eddsa.KeyPair | ||
|
||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
val ed25519 = eddsa("ed25519") | ||
val decoded = nativeValue.decodeToString().base64UrlDecodedBytes | ||
|
||
keyPair = ed25519.keyFromPublic(decoded) | ||
} | ||
|
||
actual fun getEncoded(): ByteArray { | ||
val secret = Buffer.from(keyPair.getSecret()).toByteArray() | ||
val secretBytes = secret.base64UrlEncoded.encodeToByteArray() | ||
return Buffer.from(secretBytes).toByteArray() | ||
} | ||
|
||
actual fun verify(message: ByteArray, sig: ByteArray): Boolean { | ||
return keyPair.verify(Buffer.from(message), sig) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters