-
-
Notifications
You must be signed in to change notification settings - Fork 645
Tutorial for Signature class
Kenji Urushima edited this page May 12, 2013
·
8 revisions
TOP | DOWNLOADS | TUTORIALS | API REFERENCE | DEMOS
The KJUR.crypto.Signature class is a very similar to Java JCE [java.security.Signature] (http://docs.oracle.com/javase/7/docs/api/index.html?java/security/Signature.html) class for digital signature algorithm calculation. So it's easy to learn.
Here is a basic example for 'SHA1withRSA' signature calculation.
// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
// initialize for signature generation
sig.initSign(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", "prov": "cryptojs/jsrsa"});
// initialize for signature validation
sig.initVerifyByCertificatePEM("-----BEGIN CERTIFICATE-----(snip)"); // signer's certificate
// update data
sig.updateString('aaa')
// verify signature
var isValid = sig.verify(sigValueHex)
You can also update a hexadecimal string as hash input.
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
sig.initSign(rsaPrivateKey);
sig.updateHex('5f6de0');
var sigValueHex = sig.sign()
The 'updateHex' and 'updateString' method can be called one or more times.
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
sig.initSign(rsaPrivateKey);
sig.updateHex('5f6de0');
sig.updateHex('9a3bcd345793173');
sig.updateHex('5f6de0');
sig.updateString('abcdefg');
sig.updateHex('01341571fg56ab');
sig.updateString('apple');
var sigValueHex = sig.sign()
To update and digest in a one method you can use 'signHex' or 'signString' method.
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
sig.initSign(rsaPrivateKey);
var sigValueHex = sig.signString('aaa')
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
sig.initSign(rsaPrivateKey);
var sigValueHex = sig.signHex('1bdeff')
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
To use Signature class following codes will be required.
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha1.js"></script>
<script src="http://kjur.github.io/jsrsasign/jsbn.js"></script>
<script src="http://kjur.github.io/jsrsasign/jsbn2.js"></script>
<script src="http://kjur.github.io/jsrsasign/rsa.js"></script>
<script src="http://kjur.github.io/jsrsasign/rsa2.js"></script>
<script src="http://kjur.github.io/jsrsasign/crypto-1.0.js"></script>
- CryptoJS Library - progressive hashing supported crypto library