Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: Crypto, KeyPair, PrivateKey, PublicKey #6

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 102 additions & 16 deletions src/test/kotlin/com/nftco/flow/sdk/crypto/CryptoTest.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,116 @@
package com.nftco.flow.sdk.crypto

import com.nftco.flow.sdk.HashAlgorithm
import com.nftco.flow.sdk.SignatureAlgorithm
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.math.BigInteger
import java.security.Signature

internal class CryptoTest {
@Test
fun `Can generate KeyPair`() {
val pair1 = Crypto.generateKeyPair(SignatureAlgorithm.ECDSA_P256)
val privateKey1 = Crypto.decodePrivateKey(pair1.private.hex)
val publicKey1 = Crypto.decodePublicKey(pair1.public.hex)
assertThat(pair1.private.hex).isEqualTo(privateKey1.hex)
assertThat(pair1.public.hex).isEqualTo(publicKey1.hex)
val keyPair = Crypto.generateKeyPair()
assertNotNull(keyPair.private)
assertNotNull(keyPair.public)
}

@Test
fun `Test generating different key pairs`() {
val keyPair1 = Crypto.generateKeyPair()
val keyPair2 = Crypto.generateKeyPair()

assertNotEquals(keyPair1.private, keyPair2.private)
assertNotEquals(keyPair1.public, keyPair2.public)
}

@Test
fun `Can decode private key`() {
val keyPair = Crypto.generateKeyPair()
val decodedPrivateKey = Crypto.decodePrivateKey(keyPair.private.hex)
assertNotNull(decodedPrivateKey)
assertEquals(keyPair.private.hex, decodedPrivateKey.hex)
}

@Test
fun `Private key throws exception when invalid`() {
assertThrows(IllegalArgumentException::class.java) {
Crypto.decodePrivateKey("invalidKey")
}
}

@Test
fun `Can decode public key`() {
val keyPair = Crypto.generateKeyPair()
val decodedPublicKey = Crypto.decodePublicKey(keyPair.public.hex)
assertNotNull(decodedPublicKey)
assertEquals(keyPair.public.hex, decodedPublicKey.hex)
}

@Test
fun `Public key throws exception when invalid`() {
assertThrows(IllegalArgumentException::class.java) {
Crypto.decodePublicKey("invalidKey")
}
}

@Test
fun `Get signer`() {
val keyPair = Crypto.generateKeyPair()
val signer = Crypto.getSigner(keyPair.private)
assertNotNull(signer)
}
@Test
fun `Get hasher`() {
val hasher = Crypto.getHasher()
assertNotNull(hasher)
}

@Test
fun `Test normalizeSignature`() {
val keyPair = Crypto.generateKeyPair()

val ecdsaSign = Signature.getInstance(HashAlgorithm.SHA3_256.id)
ecdsaSign.initSign(keyPair.private.key)
ecdsaSign.update("test".toByteArray())

val signature = ecdsaSign.sign()

val normalizedSignature = Crypto.normalizeSignature(signature, keyPair.private.ecCoupleComponentSize)

val expectedLength = 2 * keyPair.private.ecCoupleComponentSize
assertEquals(expectedLength, normalizedSignature.size)
}

val pair2 = Crypto.generateKeyPair(SignatureAlgorithm.ECDSA_P256)
val privateKey2 = Crypto.decodePrivateKey(pair2.private.hex)
val publicKey2 = Crypto.decodePublicKey(pair2.public.hex)
assertThat(pair2.private.hex).isEqualTo(privateKey2.hex)
assertThat(pair2.public.hex).isEqualTo(publicKey2.hex)
@Test
fun `Test extractRS`() {
val keyPair = Crypto.generateKeyPair()

val ecdsaSign = Signature.getInstance(HashAlgorithm.SHA3_256.id)
ecdsaSign.initSign(keyPair.private.key)

println(keyPair.private.key)
ecdsaSign.update("test".toByteArray())

val signature = ecdsaSign.sign()

val (r, s) = Crypto.extractRS(signature)

assertTrue(r > BigInteger.ZERO)
assertTrue(s > BigInteger.ZERO)
}

@Test
fun `Hasher implementation`() {
val hasher = HasherImpl(HashAlgorithm.SHA3_256)
val hashedBytes = hasher.hash("test".toByteArray())
assertNotNull(hashedBytes)
}

@Test
fun `Can sign stuff`() {
val pair = Crypto.generateKeyPair()
val signer = Crypto.getSigner(pair.private, HashAlgorithm.SHA3_256)
signer.sign("testing".toByteArray())
fun `Signer implementation`() {
val keyPair = Crypto.generateKeyPair()
val signer = SignerImpl(keyPair.private, HashAlgorithm.SHA3_256)
val signature = signer.sign("test".toByteArray())
assertNotNull(signature)
}
}
Loading