Updated on 2026-08-14
This commit is contained in:
parent
4c64438643
commit
66a56a7344
16 changed files with 383 additions and 172 deletions
|
|
@ -0,0 +1,92 @@
|
|||
package com.tangem.common.extensions
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
import java.util.*
|
||||
|
||||
|
||||
class ByteArrayExtensionsTest {
|
||||
|
||||
@Test
|
||||
fun `card Id to Hex String`() {
|
||||
val hex = "cb22000000027374"
|
||||
val bytes = byteArrayOf(-53, 34, 0, 0, 0, 2, 115, 116)
|
||||
assertThat(bytes.toHexString())
|
||||
.matches(hex)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `batch Id to Hex String`() {
|
||||
val hex = "0029"
|
||||
val bytes = byteArrayOf(0, 41)
|
||||
assertThat(bytes.toHexString())
|
||||
.matches(hex)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `curve name to Utf8`() {
|
||||
val bytes = byteArrayOf(115, 101, 99, 112, 50, 53, 54, 107, 49, 0)
|
||||
val expected = "secp256k1"
|
||||
val converted = bytes.toUtf8()
|
||||
assertThat(converted)
|
||||
.matches(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `blockchain name to Utf8`() {
|
||||
val bytes = byteArrayOf(69, 84, 72)
|
||||
val expected = "ETH"
|
||||
val converted = bytes.toUtf8()
|
||||
assertThat(converted)
|
||||
.matches(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `bytes to int`() {
|
||||
val bytes = byteArrayOf(0, 2, 106, 3)
|
||||
val expected = 158211
|
||||
assertThat(bytes.toInt())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `zero to int`() {
|
||||
val bytes = byteArrayOf(0)
|
||||
val expected = 0
|
||||
assertThat(bytes.toInt())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toDate() {
|
||||
val bytes1 = byteArrayOf(7, -30, 7, 27)
|
||||
val expected1 = Calendar.getInstance().apply { this.set(2018, 6, 27, 0, 0, 0) }.time
|
||||
val converted1 = bytes1.toDate()
|
||||
assertThat(converted1.toString())
|
||||
.isEqualTo(expected1.toString())
|
||||
|
||||
val bytes2 = byteArrayOf(7, -30, 7, 27, 30)
|
||||
val expected2 = Calendar.getInstance().apply { this.set(2018, 6, 27, 0, 0, 0) }.time
|
||||
val converted2 = bytes2.toDate()
|
||||
assertThat(converted2.toString())
|
||||
.isEqualTo(expected2.toString())
|
||||
|
||||
val bytes3 = byteArrayOf(7, -30, 7)
|
||||
val expected3 = Calendar.getInstance().apply { this.set(2018, 6, 0, 0, 0, 0) }.time
|
||||
val converted3 = bytes3.toDate()
|
||||
assertThat(converted3.toString())
|
||||
.isEqualTo(expected3.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `calculate sha512`() {
|
||||
val bytes = ByteArray(64) { 5 }
|
||||
val expected = byteArrayOf(
|
||||
-123, 96, 121, 57, -117, -23, -108, 57, 25, -119, -22, 97, 11, -91,
|
||||
74, -19, -88, 21, -108, -116, -100, 111, 6, -78, 114, -115, 70, -121, 29, 102, 104, 65,
|
||||
-21, -68, -111, 121, -51, 109, -94, -24, -40, 108, -25, 70, -26, 61, 38, 12, -127, -34,
|
||||
-77, -81, 81, -32, -89, -112, -31, -33, 91, 114, 89, 127, -123, -58)
|
||||
assertThat(bytes.calculateSha512())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package com.tangem.common.extensions
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
class StringExtensionsTest {
|
||||
|
||||
@Test
|
||||
fun `calculate SHA 256 for default PIN 1`() {
|
||||
val pin = "000000"
|
||||
val expected = byteArrayOf(-111, -76, -47, 66, -126, 63, 125, 32, -59, -16, -115, -10, -111,
|
||||
34, -34, 67, -13, 95, 5, 122, -104, -115, -106, 25, -10, -45, 19, -124, -123, -55, -94, 3)
|
||||
assertThat(pin.calculateSha256())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `calculate SHA 256 for default PIN 2`() {
|
||||
val pin = "000"
|
||||
val expected = byteArrayOf(42, -55, -90, 116, 106, -54, 84, 58, -8, -33, -13, -104, -108, -49,
|
||||
-24, 23, 58, -5, -94, 30, -80, 28, 111, -82, 51, -43, 41, 71, 34, 40, 85, -17)
|
||||
assertThat(pin.calculateSha256())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `calculate SHA 256 for a sample PIN 1`() {
|
||||
val pin = "999999"
|
||||
val expected = byteArrayOf(-109, 115, 119, -16, 86, 22, 15, -60, -79, 94, 11, 119, 12, 103,
|
||||
19, 106, 95, 3, -63, 82, 5, -76, -45, -65, -111, -126, 104, -2, -6, 44, 109, 10)
|
||||
assertThat(pin.calculateSha256())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `calculate SHA 256 for a sample PIN 2`() {
|
||||
val pin = "999"
|
||||
val expected = byteArrayOf(-125, -49, -117, 96, -99, -26, 0, 54, -88, 39, 123, -48, -23, 97,
|
||||
53, 117, 27, -68, 7, -21, 35, 66, 86, -44, -74, 91, -119, 51, 96, 101, 27, -14)
|
||||
assertThat(pin.calculateSha256())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `card ID hex to bytes`() {
|
||||
val cardId = "cb22000000027374"
|
||||
val expected = byteArrayOf(-53, 34, 0, 0, 0, 2, 115, 116)
|
||||
assertThat(cardId.hexToBytes())
|
||||
.isEqualTo(expected)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.tangem.crypto
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.tangem.commands.EllipticCurve
|
||||
import com.tangem.crypto.CryptoUtils.generatePublicKey
|
||||
import com.tangem.crypto.CryptoUtils.generateRandomBytes
|
||||
import com.tangem.crypto.CryptoUtils.verify
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
|
||||
class CryptoUtilsTest {
|
||||
|
||||
@BeforeEach
|
||||
internal fun setUp() {
|
||||
CryptoUtils.initCrypto()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun generateRandomBytesTest() {
|
||||
val privateKey: ByteArray = generateRandomBytes(32)
|
||||
assertThat(privateKey)
|
||||
.hasLength(32)
|
||||
assertThat(privateKey.sum())
|
||||
.isNotEqualTo(0)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun verifyEd25519Test() {
|
||||
val verified = verifySignature_withSampleData(EllipticCurve.Ed25519)
|
||||
assertThat(verified)
|
||||
.isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun verifySecp256k1Test() {
|
||||
val verified = verifySignature_withSampleData(EllipticCurve.Secp256k1)
|
||||
assertThat(verified)
|
||||
.isTrue()
|
||||
}
|
||||
|
||||
private fun verifySignature_withSampleData(curve: EllipticCurve): Boolean {
|
||||
val privateKey = ByteArray(32) { 1 }
|
||||
val publicKey = generatePublicKey(privateKey, curve)
|
||||
val message = ByteArray(64) { 5 }
|
||||
val signature = message.sign(privateKey, curve)
|
||||
return verify(publicKey, message, signature, curve)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue