Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit tests that were used in the XWallet app
import io.scintillamlabs.swallet.crypto.CryptoUtils
import io.scintillamlabs.swallet.crypto.CryptoUtils.SHA256Hex
import io.scintillamlabs.swallet.crypto.CryptoUtils.getPrivateKeyFromHex
import io.scintillamlabs.swallet.crypto.CryptoUtils.getPublicKeyFromHex
import io.scintillamlabs.swallet.utility.toHex
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test
import org.spongycastle.jce.provider.BouncyCastleProvider
import org.spongycastle.util.encoders.Hex
import java.security.Security
/**
* Created by eddylloyd on 12/30/17.
* Saturday 11:11 PM
*/
class CryptoUtilityTests {
init {
Security.addProvider(BouncyCastleProvider())
}
@Test
fun verifySignatureShouldPass() {
val vSig = CryptoUtils.verifySignature(
pubKeyHex = "3056301006072a8648ce3d020106052b8104000a03420004111c75eb8a908f13c7ddf23d439e79ee599c701a9cb8a77c533a3a53d1a0bfbbe30406aff91a635b089c49071b613c08d73a85bacfb6dcb6729ad82dda9ff8e7",
signatureHex = "3046022100ea4d8dba900b06e33db46dcf88c026b37778b1aebe0eb8e3adefbd6cc930a4e9022100c46e31e8fd07183fb81a9066b275c0a94888fb02da1e4f855dad1cb1b889c3c1",
hexData = "c8ed5e65713f8170b8d0769a8ed8c35740ba0b706d5856e22d709be1e001cf8c"
)
assertEquals(true, vSig)
}
@Test
fun verifySignatureShouldNotPass() {
val vSig = CryptoUtils.verifySignature(
pubKeyHex = "3056301006072a8648ce3d020106052b8104000a03420004111c75eb8a908f13c7ddf23d439e79ee599c701a9cb8a77c533a3a53d1a0bfbbe30406aff91a635b089c49071b613c08d73a85bacfb6dcb6729ad82dda9ff8e7",
signatureHex = "0046022100ea4d8dba900b06e33db46dcf88c026b37778b1aebe0eb8e3adefbd6cc930a4e9022100c46e31e8fd07183fb81a9066b275c0a94888fb02da1e4f855dad1cb1b889c3c1",
hexData = "c8ed5e65713f8170b8d0769a8ed8c35740ba0b706d5856e22d709be1e001cf8c"
)
assertEquals(false, vSig)
}
@Test
fun SHA256Tests () {
assertEquals(
(CryptoUtils.SHA256("Hello".toByteArray())).toHex(),
Hex.toHexString(CryptoUtils.SHA256("Hello".toByteArray()))
)
assertNotEquals(
(CryptoUtils.SHA256("Hello".toByteArray())),
(CryptoUtils.SHA256("Hello !".toByteArray()))
)
}
@Test
fun keyGeneration () {
val keyPair = CryptoUtils.generateKeyPair()
val privateKey = keyPair.first
val pubKey = keyPair.second
val address = CryptoUtils.getAddressFromPubKey(pubKey)
print("pvk: $privateKey \npb : $pubKey\n")
val message = SHA256Hex("Hi".toByteArray())
val signature = CryptoUtils.signMessage(message, privateKey)
print("signature: $signature")
assertEquals(
CryptoUtils.verifySignature(pubKey, signature, message),
true
)
}
@Test
fun keysRecovery () {
val keyPair = CryptoUtils.generateKeyPair()
val privateKeyHex = keyPair.first
val pubKeyHex = keyPair.second
val ecPrivateKey = getPrivateKeyFromHex(privateKeyHex)
assertEquals(
ecPrivateKey.d.toString(16),
privateKeyHex
)
val ecPubKey = getPublicKeyFromHex(pubKeyHex)
assertEquals(
ecPubKey.encoded.toHex(),
pubKeyHex
)
}
@Test
fun signingTests() {
val message = "82a3c4126adc656bc2cbcc501ed83bfb15ed48341ca1fa9bbcb6f36eccdf5d41"
val privateKey = "ec67a6f6aa475d0e696f66b24f6105c9eeb152100733b08ffeed4053c25ccb64"
val signature = CryptoUtils.signMessage(message, privateKey)
print("signature: $signature")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment