TCryptoSign
Message signing.
The nonces are non-deterministic. They are computed using the output of the CSPRNG as well as a hash of the message to be signed. As a result, signing the same message multiple times can produce different, all valid signatures. However, only the owner of the secret key can compute a valid signature.
Example 1
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
' generate a key pair
Local keyPair:TCryptoSignKeyPair = TCryptoSign.KeyGen()
Print "keypair : " + keyPair.ToString()
Local signature:TCryptoSignature
Local message:String = "Just a castaway, an island lost at sea. Another lonely day with no one here but me. More loneliness than any man could bear. Rescue me before I fall into despair."
Local mb:Byte Ptr = message.ToUTF8String()
' sign the message with the secret key
TCryptoSign.Sign(signature, mb, Size_T(message.length), "example", keyPair)
Print "signature : " + signature.ToString()
' verify the message with public key
If TCryptoSign.Verify(signature, mb, Size_T(message.length), "example", keyPair) Then
Print "Verified !"
Else
Print "Invalid"
End If
MemFree(mb)
Example 2
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
' generate a key pair
Local keyPair:TCryptoSignKeyPair = TCryptoSign.KeyGen()
Print "keypair : " + keyPair.ToString()
Local signature:TCryptoSignature
Local message:String[] = ["Just a castaway, an island lost at sea. " , ..
"Another lonely day with no one here but me. " , ..
"More loneliness than any man could bear. " , ..
"Rescue me before I fall into despair."]
' create a signer with context
Local signer:TCryptoSign = New TCryptoSign.Create("example")
' update all the parts
Update(signer, message)
' create the signature
signer.FinishCreate(signature, keyPair)
Print "signature : " + signature.ToString()
' create a verifier with context
Local verifier:TCryptoSign = New TCryptoSign.Create("example")
' update all the parts
Update(verifier, message)
' verify the public key against the signature
If verifier.FinishVerify(signature, keyPair) Then
Print "Verified !"
Else
Print "Invalid"
End If
Function Update(signer:TCryptoSign, message:String[])
For Local i:Int = 0 Until message.length
Local mb:Byte Ptr = message[i].ToUTF8String()
signer.Update(mb, Size_T(message[i].length))
MemFree(mb)
Next
End Function
Methods
Method Create:TCryptoSign(context:String)
Initializes a state state using the context.
Method Update:Int(m:Byte Ptr, mLen:Size_T)
Hashes chunk m of mLen bytes.
Method Update:Int(m:Byte[])
Hashes chunk m.
Method FinishCreate:Int(csig:TCryptoSignature Var, kp:TCryptoSignKeyPair)
Computes the signature into csig using the secret key from kp.
Method FinishVerify:Int(csig:TCryptoSignature, kp:TCryptoSignKeyPair)
Verifies the signature into csig using the public key from kp.
Returns False if the signature doesn't appear to be valid for the given message, context and public key, or True if it could be successfully verified.
Functions
Function KeyGen:TCryptoSignKeyPair()
Generates a secret key and a corresponding public key.
Function Sign:Int(csig:TCryptoSignature Var, m:Byte Ptr, mLen:Size_T, context:String, kp:TCryptoSignKeyPair)
Computes a signature for a message m whose length is mLen bytes, using the secret key from kp and a context.
Function Sign:Int(csig:TCryptoSignature Var, m:Byte[], context:String, kp:TCryptoSignKeyPair)
Computes a signature for a message m, using the secret key from kp and a context.
Function Verify:Int(csig:TCryptoSignature, m:Byte Ptr, mLen:Size_T, context:String, kp:TCryptoSignKeyPair)
Checks that the signed message m whose length is mLen bytes has a valid signature for the public key pk and the context.
If the signature is doesn't appear to be valid, the function returns False. On success, it returns True.
Function Verify:Int(csig:TCryptoSignature, m:Byte[], context:String, kp:TCryptoSignKeyPair)
Checks that the signed message m has a valid signature for the public key pk and the context.
If the signature is doesn't appear to be valid, the function returns False. On success, it returns True.