Skip to content

Commit

Permalink
Fix ECH sample with proper signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
forceporquillo committed Jun 12, 2021
1 parent 65233d3 commit ac59b21
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/main/java/org/creativeturbogiants/crypto/Main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.creativeturbogiants.crypto

import org.creativeturbogiants.crypto.curve25519.Curve25519
import org.creativeturbogiants.crypto.utils.bytesToHex

fun main(args: Array<String>) {

Expand All @@ -15,45 +16,36 @@ fun main(args: Array<String>) {
val secret2 = cipher.calculateAgreement(keyPair2.publicKey, keyPair1.privateKey)

println("---------------------------------------------------------------------------------------------")
println("User 1 public key = ${bytesToHex(keyPair1.publicKey)}")
println("User 1 private key = ${bytesToHex(keyPair1.privateKey)}")
println("Alice")
println("Public key = ${bytesToHex(keyPair1.publicKey)}")
println("Private key = ${bytesToHex(keyPair1.privateKey)}")

println("---------------------------------------------------------------------------------------------")
println("User 2 public key = ${bytesToHex(keyPair2.publicKey)}")
println("User 2 private key = ${bytesToHex(keyPair2.privateKey)}")
println("Bob")
println("public key = ${bytesToHex(keyPair2.publicKey)}")
println("private key = ${bytesToHex(keyPair2.privateKey)}")

println("---------------------------------------------------------------------------------------------")
println("User 1 shared secret key = ${bytesToHex(secret1)}")
println("User 2 shared secret key = ${bytesToHex(secret2)}")
println("Alice shared key = ${bytesToHex(secret1)}")
println("Bob shared key = ${bytesToHex(secret2)}")

println("---------------------------------------------------------------------------------------------")
val signature = cipher.calculateSignature(
keyPair1.privateKey, "test".toByteArray()
)

println("Message Signature : ${bytesToHex(signature)}")
println("Signature : ${bytesToHex(signature)}")

val isValidSignature = cipher.verifySignature(
keyPair1.publicKey, "test".toByteArray(), signature
)

println("Message Signature Valid : $isValidSignature")
println("Valid : $isValidSignature")

val sig = bytesToHex(signature)
println("Message length : ${sig.length} bit symmetric cipher ${sig.length == 128}")
println("Message in bytes : ${signature.size} bytes")
println(" : ${sig.length} bit symmetric cipher")
println(" : ${signature.size} bytes")

}

private val HEX_ARRAY = "0123456789ABCDEF".toCharArray()

fun bytesToHex(bytes: ByteArray): String {
val hexChars = CharArray(bytes.size * 2)
for (j in bytes.indices) {
val v = bytes[j].toInt() and 0xFF
hexChars[j * 2] = HEX_ARRAY[v.ushr(4)]
hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F]
}

return String(hexChars)
}

0 comments on commit ac59b21

Please sign in to comment.