CryptoKey
The CryptoKey
class in the 4D language encapsulates an asymmetric encryption key pair.
This class is available from the 4D
class store.
For a comprehensive overview of this class, please refer to the CryptoKey: encrypt, decrypt, sign, and verify! blog post.
Summary
4D.CryptoKey.new( settings : Object ) : 4D.CryptoKey creates a new 4D.CryptoKey object encapsulating an encryption key pair |
.curve : Text normalised curve name of the key |
.decrypt( message : Text ; options : Object ) : Object decrypts the message parameter using the private key |
.encrypt( message : Text ; options : Object ) : Text encrypts the message parameter using the public key |
.getPrivateKey() : Text returns the private key of the CryptoKey object |
.getPublicKey() : Text returns the public key of the CryptoKey object |
.sign (message : Text ; options : Object) : Text signs the utf8 representation of a message string |
.size : Integer the size of the key in bits |
.type : Text name of the key type - "RSA", "ECDSA", "PEM" |
.verify( message : Text ; signature : Text ; options : Object) : object verifies the base64 signature against the utf8 representation of message |
4D.CryptoKey.new()
History
Release | Changes |
---|---|
18 R4 | Added |
4D.CryptoKey.new( settings : Object ) : 4D.CryptoKey
Parameter | Type | Description | |
---|---|---|---|
settings | Object | -> | Settings to generate or load a key pair |
result | 4D.CryptoKey | <- | Object encapsulating an encryption key pair |
The 4D.CryptoKey.new()
function creates a new 4D.CryptoKey
object encapsulating an encryption key pair, based upon the settings object parameter. It allows to generate a new RSA or ECDSA key, or to load an existing key pair from a PEM definition.
settings
Property | Type | Description |
---|---|---|
type | text | Defines the type of the key to create: |
curve | text | Name of ECDSA curve |
pem | text | PEM definition of an encryption key to load |
size | integer | Size of RSA key in bits |
CryptoKey
The returned CryptoKey
object encapsulates an encryption key pair. It is a shared object and can therefore be used by multiple 4D processes simultaneously.
Example 1
A message is signed by a private key and the signature is verified by the corresponding public key. The following code signs and verifies a simple message signature.
- Bob's side:
// Create the message
$message:="hello world"
Folder(fk desktop folder).file("message.txt").setText($message)
// Create a key
$type:=New object("type";"RSA")
$key:=4D.CryptoKey.new($type)
// Get the public key and save it
Folder(fk desktop folder).file("public.pem").setText($key.getPublicKey())
// Get signature as base64 and save it
Folder(fk desktop folder).file("signature").setText($key.sign($message;$type))
/*Bob sends the message, the public key and the signature to Alice*/
- Alice's side:
// Get message, public key & signature
$message:=Folder(fk desktop folder).file("message.txt").getText()
$publicKey:=Folder(fk desktop folder).file("public.pem").getText()
$signature:=Folder(fk desktop folder).file("signature").getText()
// Create a key
$type:=New object("type";"PEM";"pem";$publicKey)
$key:=4D.CryptoKey.new($type)
// Verify signature
If ($key.verify($message;$signature;$type).success)
// The signature is valid
End if
Example 2
The following sample code signs and verifies a message using a new ECDSA key pair, for example in order to make a ES256 JSON Web token.
// Generate a new ECDSA key pair
$key:=4D.CryptoKey.new(New object("type";"ECDSA";"curve";"prime256v1"))
// Get signature as base64
$message:="hello world"
$signature:=$key.sign($message;New object("hash";"SHA256"))
// Verify signature
$status:=$key.verify($message;$signature;New object("hash";"SHA256"))
ASSERT($status.success)