The God of sr25519 library. Combines whole logic into the one place.
On a low level has next constructor:
Copy class KeyPair (
val secretKey: SecretKey ,
val publicKey: PublicKey ,
)
While you can use this knowing Secret Key and creating Public Key out of it, there are more popular ways to work with it.
Uniform
Copy val secretKeyBytes: ByteArray
val publicKeyBytes: ByteArray
val keyPair: KeyPair = KeyPair. fromByteArray (secretKeyBytes + secretKeyBytes)
val secretKeyAndRistretto = keyPair. toByteArray ()
ed25519
Copy val secretKeyBytes: ByteArray
val publicKeyBytes: ByteArray
val keyPair: KeyPair = KeyPair. fromHalfEd25519ByteArray (secretKeyBytes + secretKeyBytes)
val secretKeyAndRistretto = keyPair. toHalfEd25519ByteArray ()
Generation
Basically, proxies the call to internal Secret Key:
Copy val generatedKeyPair: KeyPair = KeyPair. generate ()
val yourSecureRandom: kotlin .random.Random
val generatedKeyPairWithCPSRNG: KeyPair = KeyPair. generateWith (yourSecureRandom)
Signing
Same proxying of signature methods to its Secret Key:
Copy val yourContextLabel: String
val messageToSign: ByteArray
val signingContext: SigningContext = SigningContext
. fromContext (yourContextLabel. toByteArray ())
val signingTranscript: SigningTranscript = signingContext. bytes (messageToSign)
val yourKeyPair: KeyPair
val signedMessage: Signature = yourKeyPair. sign (signingTranscript)
Double check signature:
Copy val signingTranscript: SigningTranscript
val yourKeyPair: KeyPair
val signedMessage: Signature = yourKeyPair. signDoubleCheck (signingTranscript)
Simple signing:
Copy val yourContextLabel: String
val messageToSign: ByteArray
// Still, we need to set the context
val signingContext: SigningContext = SigningContext
. fromContext (yourContextLabel. toByteArray ())
val yourKeyPair: KeyPair
val signedMessage: Signature = yourKeyPair. signSimple (signingContext, messageToSign)
val doubleCheckedSignedMessage: Signature = yourKeyPair. signSimpleDoubleCheck (signingContext, messageToSign)
Uniform
Verifying
Now it has proxy methods for its internal Public Key:
Copy val yourKeyPair: KeyPair
val message: ByteArray
val signedMessage: ByteArray
val signature: Signature = Signature. fromByteArray (signedMessage)
val signingContext: SigningContext = SigningContext
. fromContext (yourContextLabel. toByteArray ())
val simplyVerified: Boolean = yourKeyPair. verifySimple (signingContext, message, signature)
val signingTranscript: SigningTranscript = signingContext. bytes (message)
val verified: Boolean = yourKeyPair. verify (signingTranscript, signature)
// simplyVerified == verified