Skip to content

Tutorial for Signature class

Kenji Urushima edited this page Nov 4, 2021 · 8 revisions

TOP | DOWNLOADS | TUTORIALS | API REFERENCE | DEMOS


The KJUR.crypto.Signature class is a very similar to Java JCE java.security.Signature class for digital signature algorithm calculation. So it's easy to learn.

Getting Started

Here is a basic example for 'SHA1withRSA' signature calculation.

// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
// initialize for signature generation
sig.init(rsaPrivateKey);   // rsaPrivateKey of RSAKey object
// update data
sig.updateString('aaa')
// calculate signature
var sigValueHex = sig.sign()

Here is a example for signature validation.

// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
// initialize for signature validation
sig.init("-----BEGIN CERTIFICATE-----(snip)"); // signer's certificate
// update data
sig.updateString('aaa')
// verify signature
var isValid = sig.verify(sigValueHex)

for hexadecimal input

You can also update a hexadecimal string as hash input.

var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
sig.init(rsaPrivateKey);
sig.updateHex('5f6de0');
var sigValueHex = sig.sign()

progressive signing

The 'updateHex' and 'updateString' method can be called one or more times.

var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
sig.init(rsaPrivateKey);
sig.updateHex('5f6de0');
sig.updateHex('9a3bcd345793173');
sig.updateHex('5f6de0');
sig.updateString('abcdefg');
sig.updateHex('01341571fg56ab');
sig.updateString('apple');
var sigValueHex = sig.sign()

in short

To update and digest in a one method you can use 'signHex' or 'signString' method.

var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
sig.init(rsaPrivateKey);
var sigValueHex = sig.signString('aaa')

var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
sig.init(rsaPrivateKey);
var sigValueHex = sig.signHex('1bdeff')

supported cryptographic providers and signature algorithms

Here is a list of supported cryptographic providers and signature algorithms.

  • cryptojs/jsrsa - MD5withRSA
  • cryptojs/jsrsa - SHA1withRSA
  • cryptojs/jsrsa - SHA224withRSA
  • cryptojs/jsrsa - SHA256withRSA
  • cryptojs/jsrsa - SHA384withRSA
  • cryptojs/jsrsa - SHA512withRSA
  • cryptojs/jsrsa - RIPEMD160withRSA

required JavaScript sources

To use Signature class following codes will be required.

<script src="http://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js"></script>

Reference

Clone this wiki locally