-
-
Notifications
You must be signed in to change notification settings - Fork 644
Tutorial for JWS generation
TOP(jsrsasign) | WIKI | DOWNLOADS | TUTORIALS | API REFERENCE | DEMOS
To use jsrsasign including jsjws on your browser, just include 'jsrsasign-latest-all-min.js' script as following:
<script language="JavaScript" type="text/javascript"
src="http://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js">
</script>
To generate a RFC 7515 JSON Web Signature(JWS), first you may prepare 'Header' and 'Payload'. The header will be an JSON object which has signed properties such like 'alg' for signature algorithm. As for payload, any string can be used.
var oHeader = {alg: "HS256"};
var sHeader = JSON.stringify(oHeader);
var sPayload = "aaa";
Then you should prepare HMAC shared key or private key for public key cryptography. For example, to generate HS256 HMAC JWS signature, you should prepare HMAC key in hexadecimal string. In this example, the key is '616161' which is 'aaa' string. Then you can sign JWS.
var sJWS = KJUR.jws.JWS.sign("HS256", sHeader, sPayload, "616161");
In order to use RSA algorithm or RSA-PSS algorithm for signing, you may prepare RSAKey object of private key. KEYUTIL.getKey method can be used to load PKCS#1 or PKCS#8 PEM formatted encrypted or plain RSA/DSA/ECC private key. Here is an example for RSA key:
var prvKey = KEYUTIL.getKey(sRSAPRV_PKCS8PEM, "password");
var sJWS = KJUR.jws.JWS.sign("RS256", JSON.stringify({alg: "RS256"}),
sPayload, prvKey);
As for RSA-PSS, the same as well:
var sJWS = KJUR.jws.JWS.sign("PS256", JSON.stringify({alg: "PS256"}),
sPayload, prvKey);
As for ECC ECDSA, code will be following:
var prvKey = KEYUTIL.getKey(sECCPRV_PKCS8PEM, "password");
var sJWS = KJUR.jws.JWS.sign("ES256", JSON.stringify({alg: "ES256"}),
sPayload, prvKey);