Skip to content

TWebRSASignature

TWebRSASignature is a wrapper around the Web Crypto API. It's recommended to first familiarize yourself with the Web Crypto API: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

The Web Crypto API provides native support to create, use and store cryptographic keys without exposing the content of private keys.

Below is a list of the most important properties, methods and events for the TWebRSASignature. The supported algorithm is: RSASSA-PKCS1-v1_5.

See TWebAESEncryption documentation on how to create keys.

Sign data

The available Sign methods are event-based. They will trigger the OnSigned event when they are ready. Use the SignP promise-based functions if you need to wait for a sign process to finish.

//Signing with SignP
procedure TForm1.WebButton1Click(Sender: TObject);
var
  ab: TJSArrayBuffer;
  I: Integer;
  res: string;
begin
  for I := 0 to 9 do
  begin
    ab := Await(TJSArrayBuffer, rsaSign.SignP(myData[I]));
    //Do something with ab
    //Convert to HEX for example, before sending to a server:
    res := ABToHex(ab);
  end;
end;

Verify data

The available Verify method is event-based. It will trigger the OnVerify event when it has finished verifying. Use the VerifyP promise-based function if you need to wait for an verification to finish.

//Verifying with VerifyP
procedure TForm1.WebButton1Click(Sender: TObject);
var
  res: Booelan;
  mySignature, myData: TJSArrayBuffer;
begin
  //Some code...
  res := Await(Boolean, rsaSign.VerifyP(mySignature, myData));
  //if res = True, the signature is valid
  //Some more code...
end;

Properties for TWebRSASignature

Property Description
ExtractableKey: Boolean Determines if the key is extractable. If modified, it's not applied to the current key.
Hash: TCryptoHash The hash function to be used with the algorithm. If modified, it's not applied to the current key.
ModulusLength: TRSAModulusLength The length in bits of the RSA modulus. If modified, it's not applied to the current key.
PrivateKey: TJSCryptoKey The private CryptoKey object.
PublicKey: TJSCryptoKey The public CryptoKey object.
Usages Set of key usages. If modified, it's not applied to the current key.

Methods for TWebRSASignature

Property Description
ExportKey(AKeyType: TCryptoAsymKeyType; AFormat: TCryptoExportImportFormat) Method to export the class's keys. AKeyTyperepresents which key to export. The supported formats are PKCS#8 (PEM encoded string) for private keys, SPKI (PEM encoded string) for public keys, and jwk (JSON string) for private/public keys.
ExportKeyP(AKeyType: TCryptoAsymKeyType; AFormat: TCryptoExportImportFormat): TJSPromise Promise-based equivalent of ExportKey. Resolves with a string value.
GenerateKey Generates a new key pair based on the current property settings.
GenerateKeyP: TJSPromise Promise-based equivalent of GenerateKey. Resolves with a True value.
ImportKey(AKey: string; AKeyType: TCryptoAsymKeyType; AFormat: TCryptoExportImportFormat) Method to import a string formatted key. AKeyType determines which key to import (private/public). AFormat should be PKCS#8/jwk in case of a private key and SPKI/jwk in case of a public key.
ImportKeyP(AKey: string; AKeyType: TCryptoAsymKeyType; AFormat: TCryptoExportImportFormat): TJSPromise Promise-based equivalent of ImportKey(AKey). Resolves with a True value.
ImportKey(ABinary: TJSUint8Array; AKeyType: TCryptoAsymKeyType) Method to import a key stored in binary format. Will automatically use PKCS#8 for a private key and SPKI for a public key.
ImportKeyP(ABinary: TJSUint8Array; AKeyType: TCryptoAsymKeyType): TJSPromise Promise-based equivalent of ImportKey(ABinary). Resolves with a True value.
Sign(AText: string) Sign AText with the class’s public key and algorithm.
SignP(AText: string): TJSPromise Promise-based equivalent of Sign(AText). Resolves with a TJSArrayBuffer value.
Sign(ABinary: TJSUint8Array) Sign ABinary with the class’s public key and algorithm.
SignP(ABinary: TJSUint8Array): TJSPromise Promise-based equivalent of Sign(ABinary). Resolves with a TJSArrayBuffervalue.
Verify(ASignature: TJSArrayBuffer; AData: TJSArrayBuffer) Verify AData with ASignature, using the class’s private key and algorithm
VerifyP(ASignature: TJSArrayBuffer; AData: TJSArrayBuffer): TJSPromise Promise-based equivalent of Verify. Resolves with a Boolean value.

Events for TWebRSASignature

Property Description
OnError Event triggered when there's a Promise rejection.
OnKeyCreated Event triggered when a key is created.
OnKeyExportedJSON Event triggered when a key is exported as a JSON string.
OnKeyExportedPKCS8 Event triggered when a key is exported in PKCS#8 format as a PEM encoded string.
OnKeyExportedSPKI Event triggered when a key is exported in SPKI format as a PEM encoded string.
OnPrivateKeyImported Event triggered when a private key is imported.
OnPublicKeyImported Event triggered when a public key is imported.
OnSigned Event triggered when data is signed.
OnVerify Event triggered when data is verified