Javascript crypto.signText() Demo/Interop

Use the Redwax SignText web extension to allow signing of arbitrary text using a digital certificate.

Javascript crypto.signText()

The following example form shows how to trigger crypto.signText() to sign the given text.

Html Form

To request text be signed, submit the form below.

crypto.signText() returns a Promise which, when resolved, contains the signed text.

Detection

To detect presence of the API, call crypto.signText() without any parameters. If you receive a Promise in return, the new API is present..

Legacy Form

The original crypto.signText() as implemented by Netscape and then Firefox returned a string instead of a Promise. The following code shows the legacy behaviour, and should not be used in modern code.

Combined Form

The following form detects the return value from crypto.signText(). If we detect a string, we populate the field immediately, otherwise we resolve the promise returned. This form should work on both new and legacy browsers.

Javascript

The javascript used to trigger the crypto.signText() functionality can be downloaded here, and is included for reference below.

function signRequest(form) {
  if (crypto.signText) {
    crypto.signText(form['tobesigned'].value).then(response => { form['signed'].value = response });
  }
  else {
    alert('An implementation of crypto.signText() is not installed in this browser.');
  }
}

function detectRequest(form) {
  if (crypto.signText) {
    crypto.signText().then(response => form['detected'].value = response.name + ' v' + response.version);
  }
  else {
    alert('An implementation of crypto.signText() is not installed in this browser.');
  }
}

function signLegacyRequest(form) {
  if (crypto.signText) {
    form['legacysigned'].value = crypto.signText(form['tobelegacysigned'].value, 'ask');
    return true;
  }
  else {
    alert('An implementation of crypto.signText() is not installed in this browser.');
  }
}

function signCombinedRequest(form) {
  if (crypto.signText) {
    value = crypto.signText(form['tobecombinedsigned'].value, 'ask');
    if (typeof value === 'string') {
      form['combinedsigned'].value = value;
    }
    else {
      value.then(response => { form['combinedsigned'].value = response }).catch(error => {alert(error)});
    }
    return true;
  }
  else {
    alert('An implementation of crypto.signText() is not installed in this browser.');
  }
}