1 /*! crypto-1.1.8.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
  2  */
  3 /*
  4  * crypto.js - Cryptographic Algorithm Provider class
  5  *
  6  * Copyright (c) 2013-2016 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * http://kjur.github.com/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name crypto-1.1.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version 1.1.8 (2016-Feb-28)
 20  * @since jsrsasign 2.2
 21  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 /** 
 25  * kjur's class library name space
 26  * @name KJUR
 27  * @namespace kjur's class library name space
 28  */
 29 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 30 /**
 31  * kjur's cryptographic algorithm provider library name space
 32  * <p>
 33  * This namespace privides following crytpgrahic classes.
 34  * <ul>
 35  * <li>{@link KJUR.crypto.MessageDigest} - Java JCE(cryptograhic extension) style MessageDigest class</li>
 36  * <li>{@link KJUR.crypto.Signature} - Java JCE(cryptograhic extension) style Signature class</li>
 37  * <li>{@link KJUR.crypto.Util} - cryptographic utility functions and properties</li>
 38  * </ul>
 39  * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
 40  * </p>
 41  * @name KJUR.crypto
 42  * @namespace
 43  */
 44 if (typeof KJUR.crypto == "undefined" || !KJUR.crypto) KJUR.crypto = {};
 45 
 46 /**
 47  * static object for cryptographic function utilities
 48  * @name KJUR.crypto.Util
 49  * @class static object for cryptographic function utilities
 50  * @property {Array} DIGESTINFOHEAD PKCS#1 DigestInfo heading hexadecimal bytes for each hash algorithms
 51  * @property {Array} DEFAULTPROVIDER associative array of default provider name for each hash and signature algorithms
 52  * @description
 53  */
 54 KJUR.crypto.Util = new function() {
 55     this.DIGESTINFOHEAD = {
 56 	'sha1':      "3021300906052b0e03021a05000414",
 57         'sha224':    "302d300d06096086480165030402040500041c",
 58 	'sha256':    "3031300d060960864801650304020105000420",
 59 	'sha384':    "3041300d060960864801650304020205000430",
 60 	'sha512':    "3051300d060960864801650304020305000440",
 61 	'md2':       "3020300c06082a864886f70d020205000410",
 62 	'md5':       "3020300c06082a864886f70d020505000410",
 63 	'ripemd160': "3021300906052b2403020105000414",
 64     };
 65 
 66     /*
 67      * @since crypto 1.1.1
 68      */
 69     this.DEFAULTPROVIDER = {
 70 	'md5':			'cryptojs',
 71 	'sha1':			'cryptojs',
 72 	'sha224':		'cryptojs',
 73 	'sha256':		'cryptojs',
 74 	'sha384':		'cryptojs',
 75 	'sha512':		'cryptojs',
 76 	'ripemd160':		'cryptojs',
 77 	'hmacmd5':		'cryptojs',
 78 	'hmacsha1':		'cryptojs',
 79 	'hmacsha224':		'cryptojs',
 80 	'hmacsha256':		'cryptojs',
 81 	'hmacsha384':		'cryptojs',
 82 	'hmacsha512':		'cryptojs',
 83 	'hmacripemd160':	'cryptojs',
 84 
 85 	'MD5withRSA':		'cryptojs/jsrsa',
 86 	'SHA1withRSA':		'cryptojs/jsrsa',
 87 	'SHA224withRSA':	'cryptojs/jsrsa',
 88 	'SHA256withRSA':	'cryptojs/jsrsa',
 89 	'SHA384withRSA':	'cryptojs/jsrsa',
 90 	'SHA512withRSA':	'cryptojs/jsrsa',
 91 	'RIPEMD160withRSA':	'cryptojs/jsrsa',
 92 
 93 	'MD5withECDSA':		'cryptojs/jsrsa',
 94 	'SHA1withECDSA':	'cryptojs/jsrsa',
 95 	'SHA224withECDSA':	'cryptojs/jsrsa',
 96 	'SHA256withECDSA':	'cryptojs/jsrsa',
 97 	'SHA384withECDSA':	'cryptojs/jsrsa',
 98 	'SHA512withECDSA':	'cryptojs/jsrsa',
 99 	'RIPEMD160withECDSA':	'cryptojs/jsrsa',
100 
101 	'SHA1withDSA':		'cryptojs/jsrsa',
102 	'SHA224withDSA':	'cryptojs/jsrsa',
103 	'SHA256withDSA':	'cryptojs/jsrsa',
104 
105 	'MD5withRSAandMGF1':		'cryptojs/jsrsa',
106 	'SHA1withRSAandMGF1':		'cryptojs/jsrsa',
107 	'SHA224withRSAandMGF1':		'cryptojs/jsrsa',
108 	'SHA256withRSAandMGF1':		'cryptojs/jsrsa',
109 	'SHA384withRSAandMGF1':		'cryptojs/jsrsa',
110 	'SHA512withRSAandMGF1':		'cryptojs/jsrsa',
111 	'RIPEMD160withRSAandMGF1':	'cryptojs/jsrsa',
112     };
113 
114     /*
115      * @since crypto 1.1.2
116      */
117     this.CRYPTOJSMESSAGEDIGESTNAME = {
118 	'md5':		CryptoJS.algo.MD5,
119 	'sha1':		CryptoJS.algo.SHA1,
120 	'sha224':	CryptoJS.algo.SHA224,
121 	'sha256':	CryptoJS.algo.SHA256,
122 	'sha384':	CryptoJS.algo.SHA384,
123 	'sha512':	CryptoJS.algo.SHA512,
124 	'ripemd160':	CryptoJS.algo.RIPEMD160
125     };
126 
127     /**
128      * get hexadecimal DigestInfo
129      * @name getDigestInfoHex
130      * @memberOf KJUR.crypto.Util
131      * @function
132      * @param {String} hHash hexadecimal hash value
133      * @param {String} alg hash algorithm name (ex. 'sha1')
134      * @return {String} hexadecimal string DigestInfo ASN.1 structure
135      */
136     this.getDigestInfoHex = function(hHash, alg) {
137 	if (typeof this.DIGESTINFOHEAD[alg] == "undefined")
138 	    throw "alg not supported in Util.DIGESTINFOHEAD: " + alg;
139 	return this.DIGESTINFOHEAD[alg] + hHash;
140     };
141 
142     /**
143      * get PKCS#1 padded hexadecimal DigestInfo
144      * @name getPaddedDigestInfoHex
145      * @memberOf KJUR.crypto.Util
146      * @function
147      * @param {String} hHash hexadecimal hash value of message to be signed
148      * @param {String} alg hash algorithm name (ex. 'sha1')
149      * @param {Integer} keySize key bit length (ex. 1024)
150      * @return {String} hexadecimal string of PKCS#1 padded DigestInfo
151      */
152     this.getPaddedDigestInfoHex = function(hHash, alg, keySize) {
153 	var hDigestInfo = this.getDigestInfoHex(hHash, alg);
154 	var pmStrLen = keySize / 4; // minimum PM length
155 
156 	if (hDigestInfo.length + 22 > pmStrLen) // len(0001+ff(*8)+00+hDigestInfo)=22
157 	    throw "key is too short for SigAlg: keylen=" + keySize + "," + alg;
158 
159 	var hHead = "0001";
160 	var hTail = "00" + hDigestInfo;
161 	var hMid = "";
162 	var fLen = pmStrLen - hHead.length - hTail.length;
163 	for (var i = 0; i < fLen; i += 2) {
164 	    hMid += "ff";
165 	}
166 	var hPaddedMessage = hHead + hMid + hTail;
167 	return hPaddedMessage;
168     };
169 
170     /**
171      * get hexadecimal hash of string with specified algorithm
172      * @name hashString
173      * @memberOf KJUR.crypto.Util
174      * @function
175      * @param {String} s input string to be hashed
176      * @param {String} alg hash algorithm name
177      * @return {String} hexadecimal string of hash value
178      * @since 1.1.1
179      */
180     this.hashString = function(s, alg) {
181         var md = new KJUR.crypto.MessageDigest({'alg': alg});
182         return md.digestString(s);
183     };
184 
185     /**
186      * get hexadecimal hash of hexadecimal string with specified algorithm
187      * @name hashHex
188      * @memberOf KJUR.crypto.Util
189      * @function
190      * @param {String} sHex input hexadecimal string to be hashed
191      * @param {String} alg hash algorithm name
192      * @return {String} hexadecimal string of hash value
193      * @since 1.1.1
194      */
195     this.hashHex = function(sHex, alg) {
196         var md = new KJUR.crypto.MessageDigest({'alg': alg});
197         return md.digestHex(sHex);
198     };
199 
200     /**
201      * get hexadecimal SHA1 hash of string
202      * @name sha1
203      * @memberOf KJUR.crypto.Util
204      * @function
205      * @param {String} s input string to be hashed
206      * @return {String} hexadecimal string of hash value
207      * @since 1.0.3
208      */
209     this.sha1 = function(s) {
210         var md = new KJUR.crypto.MessageDigest({'alg':'sha1', 'prov':'cryptojs'});
211         return md.digestString(s);
212     };
213 
214     /**
215      * get hexadecimal SHA256 hash of string
216      * @name sha256
217      * @memberOf KJUR.crypto.Util
218      * @function
219      * @param {String} s input string to be hashed
220      * @return {String} hexadecimal string of hash value
221      * @since 1.0.3
222      */
223     this.sha256 = function(s) {
224         var md = new KJUR.crypto.MessageDigest({'alg':'sha256', 'prov':'cryptojs'});
225         return md.digestString(s);
226     };
227 
228     this.sha256Hex = function(s) {
229         var md = new KJUR.crypto.MessageDigest({'alg':'sha256', 'prov':'cryptojs'});
230         return md.digestHex(s);
231     };
232 
233     /**
234      * get hexadecimal SHA512 hash of string
235      * @name sha512
236      * @memberOf KJUR.crypto.Util
237      * @function
238      * @param {String} s input string to be hashed
239      * @return {String} hexadecimal string of hash value
240      * @since 1.0.3
241      */
242     this.sha512 = function(s) {
243         var md = new KJUR.crypto.MessageDigest({'alg':'sha512', 'prov':'cryptojs'});
244         return md.digestString(s);
245     };
246 
247     this.sha512Hex = function(s) {
248         var md = new KJUR.crypto.MessageDigest({'alg':'sha512', 'prov':'cryptojs'});
249         return md.digestHex(s);
250     };
251 
252     /**
253      * get hexadecimal MD5 hash of string
254      * @name md5
255      * @memberOf KJUR.crypto.Util
256      * @function
257      * @param {String} s input string to be hashed
258      * @return {String} hexadecimal string of hash value
259      * @since 1.0.3
260      */
261     this.md5 = function(s) {
262         var md = new KJUR.crypto.MessageDigest({'alg':'md5', 'prov':'cryptojs'});
263         return md.digestString(s);
264     };
265 
266     /**
267      * get hexadecimal RIPEMD160 hash of string
268      * @name ripemd160
269      * @memberOf KJUR.crypto.Util
270      * @function
271      * @param {String} s input string to be hashed
272      * @return {String} hexadecimal string of hash value
273      * @since 1.0.3
274      */
275     this.ripemd160 = function(s) {
276         var md = new KJUR.crypto.MessageDigest({'alg':'ripemd160', 'prov':'cryptojs'});
277         return md.digestString(s);
278     };
279 
280     /*
281      * @since 1.1.2
282      */
283     this.getCryptoJSMDByName = function(s) {
284 	
285     };
286 };
287 
288 /**
289  * MessageDigest class which is very similar to java.security.MessageDigest class
290  * @name KJUR.crypto.MessageDigest
291  * @class MessageDigest class which is very similar to java.security.MessageDigest class
292  * @param {Array} params parameters for constructor
293  * @description
294  * <br/>
295  * Currently this supports following algorithm and providers combination:
296  * <ul>
297  * <li>md5 - cryptojs</li>
298  * <li>sha1 - cryptojs</li>
299  * <li>sha224 - cryptojs</li>
300  * <li>sha256 - cryptojs</li>
301  * <li>sha384 - cryptojs</li>
302  * <li>sha512 - cryptojs</li>
303  * <li>ripemd160 - cryptojs</li>
304  * <li>sha256 - sjcl (NEW from crypto.js 1.0.4)</li>
305  * </ul>
306  * @example
307  * // CryptoJS provider sample
308  * var md = new KJUR.crypto.MessageDigest({alg: "sha1", prov: "cryptojs"});
309  * md.updateString('aaa')
310  * var mdHex = md.digest()
311  *
312  * // SJCL(Stanford JavaScript Crypto Library) provider sample
313  * var md = new KJUR.crypto.MessageDigest({alg: "sha256", prov: "sjcl"}); // sjcl supports sha256 only
314  * md.updateString('aaa')
315  * var mdHex = md.digest()
316  */
317 KJUR.crypto.MessageDigest = function(params) {
318     var md = null;
319     var algName = null;
320     var provName = null;
321 
322     /**
323      * set hash algorithm and provider
324      * @name setAlgAndProvider
325      * @memberOf KJUR.crypto.MessageDigest
326      * @function
327      * @param {String} alg hash algorithm name
328      * @param {String} prov provider name
329      * @description
330      * @example
331      * // for SHA1
332      * md.setAlgAndProvider('sha1', 'cryptojs');
333      * // for RIPEMD160
334      * md.setAlgAndProvider('ripemd160', 'cryptojs');
335      */
336     this.setAlgAndProvider = function(alg, prov) {
337 	if (alg != null && prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
338 
339 	// for cryptojs
340 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(alg) != -1 &&
341 	    prov == 'cryptojs') {
342 	    try {
343 		this.md = KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[alg].create();
344 	    } catch (ex) {
345 		throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
346 	    }
347 	    this.updateString = function(str) {
348 		this.md.update(str);
349 	    };
350 	    this.updateHex = function(hex) {
351 		var wHex = CryptoJS.enc.Hex.parse(hex);
352 		this.md.update(wHex);
353 	    };
354 	    this.digest = function() {
355 		var hash = this.md.finalize();
356 		return hash.toString(CryptoJS.enc.Hex);
357 	    };
358 	    this.digestString = function(str) {
359 		this.updateString(str);
360 		return this.digest();
361 	    };
362 	    this.digestHex = function(hex) {
363 		this.updateHex(hex);
364 		return this.digest();
365 	    };
366 	}
367 	if (':sha256:'.indexOf(alg) != -1 &&
368 	    prov == 'sjcl') {
369 	    try {
370 		this.md = new sjcl.hash.sha256();
371 	    } catch (ex) {
372 		throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
373 	    }
374 	    this.updateString = function(str) {
375 		this.md.update(str);
376 	    };
377 	    this.updateHex = function(hex) {
378 		var baHex = sjcl.codec.hex.toBits(hex);
379 		this.md.update(baHex);
380 	    };
381 	    this.digest = function() {
382 		var hash = this.md.finalize();
383 		return sjcl.codec.hex.fromBits(hash);
384 	    };
385 	    this.digestString = function(str) {
386 		this.updateString(str);
387 		return this.digest();
388 	    };
389 	    this.digestHex = function(hex) {
390 		this.updateHex(hex);
391 		return this.digest();
392 	    };
393 	}
394     };
395 
396     /**
397      * update digest by specified string
398      * @name updateString
399      * @memberOf KJUR.crypto.MessageDigest
400      * @function
401      * @param {String} str string to update
402      * @description
403      * @example
404      * md.updateString('New York');
405      */
406     this.updateString = function(str) {
407 	throw "updateString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
408     };
409 
410     /**
411      * update digest by specified hexadecimal string
412      * @name updateHex
413      * @memberOf KJUR.crypto.MessageDigest
414      * @function
415      * @param {String} hex hexadecimal string to update
416      * @description
417      * @example
418      * md.updateHex('0afe36');
419      */
420     this.updateHex = function(hex) {
421 	throw "updateHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
422     };
423 
424     /**
425      * completes hash calculation and returns hash result
426      * @name digest
427      * @memberOf KJUR.crypto.MessageDigest
428      * @function
429      * @description
430      * @example
431      * md.digest()
432      */
433     this.digest = function() {
434 	throw "digest() not supported for this alg/prov: " + this.algName + "/" + this.provName;
435     };
436 
437     /**
438      * performs final update on the digest using string, then completes the digest computation
439      * @name digestString
440      * @memberOf KJUR.crypto.MessageDigest
441      * @function
442      * @param {String} str string to final update
443      * @description
444      * @example
445      * md.digestString('aaa')
446      */
447     this.digestString = function(str) {
448 	throw "digestString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
449     };
450 
451     /**
452      * performs final update on the digest using hexadecimal string, then completes the digest computation
453      * @name digestHex
454      * @memberOf KJUR.crypto.MessageDigest
455      * @function
456      * @param {String} hex hexadecimal string to final update
457      * @description
458      * @example
459      * md.digestHex('0f2abd')
460      */
461     this.digestHex = function(hex) {
462 	throw "digestHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
463     };
464 
465     if (params !== undefined) {
466 	if (params['alg'] !== undefined) {
467 	    this.algName = params['alg'];
468 	    if (params['prov'] === undefined)
469 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
470 	    this.setAlgAndProvider(this.algName, this.provName);
471 	}
472     }
473 };
474 
475 /**
476  * Mac(Message Authentication Code) class which is very similar to java.security.Mac class 
477  * @name KJUR.crypto.Mac
478  * @class Mac class which is very similar to java.security.Mac class
479  * @param {Array} params parameters for constructor
480  * @description
481  * <br/>
482  * Currently this supports following algorithm and providers combination:
483  * <ul>
484  * <li>hmacmd5 - cryptojs</li>
485  * <li>hmacsha1 - cryptojs</li>
486  * <li>hmacsha224 - cryptojs</li>
487  * <li>hmacsha256 - cryptojs</li>
488  * <li>hmacsha384 - cryptojs</li>
489  * <li>hmacsha512 - cryptojs</li>
490  * </ul>
491  * NOTE: HmacSHA224 and HmacSHA384 issue was fixed since jsrsasign 4.1.4.
492  * Please use 'ext/cryptojs-312-core-fix*.js' instead of 'core.js' of original CryptoJS
493  * to avoid those issue.
494  * <br/>
495  * NOTE2: Hmac signature bug was fixed in jsrsasign 4.9.0 by providing CryptoJS
496  * bug workaround.
497  * <br/>
498  * Please see {@link KJUR.crypto.Mac.setPassword}, how to provide password
499  * in various ways in detail.
500  * @example
501  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA1", "pass": "pass"});
502  * mac.updateString('aaa')
503  * var macHex = md.doFinal()
504  *
505  * // other password representation 
506  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"hex":  "6161"}});
507  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"utf8": "aa"}});
508  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"rstr": "\x61\x61"}});
509  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"b64":  "Mi02/+...a=="}});
510  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"b64u": "Mi02_-...a"}});
511  */
512 KJUR.crypto.Mac = function(params) {
513     var mac = null;
514     var pass = null;
515     var algName = null;
516     var provName = null;
517     var algProv = null;
518 
519     this.setAlgAndProvider = function(alg, prov) {
520 	alg = alg.toLowerCase();
521 
522 	if (alg == null) alg = "hmacsha1";
523 
524 	alg = alg.toLowerCase();
525         if (alg.substr(0, 4) != "hmac") {
526 	    throw "setAlgAndProvider unsupported HMAC alg: " + alg;
527 	}
528 
529 	if (prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
530 	this.algProv = alg + "/" + prov;
531 
532 	var hashAlg = alg.substr(4);
533 
534 	// for cryptojs
535 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(hashAlg) != -1 &&
536 	    prov == 'cryptojs') {
537 	    try {
538 		var mdObj = KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[hashAlg];
539 		this.mac = CryptoJS.algo.HMAC.create(mdObj, this.pass);
540 	    } catch (ex) {
541 		throw "setAlgAndProvider hash alg set fail hashAlg=" + hashAlg + "/" + ex;
542 	    }
543 	    this.updateString = function(str) {
544 		this.mac.update(str);
545 	    };
546 	    this.updateHex = function(hex) {
547 		var wHex = CryptoJS.enc.Hex.parse(hex);
548 		this.mac.update(wHex);
549 	    };
550 	    this.doFinal = function() {
551 		var hash = this.mac.finalize();
552 		return hash.toString(CryptoJS.enc.Hex);
553 	    };
554 	    this.doFinalString = function(str) {
555 		this.updateString(str);
556 		return this.doFinal();
557 	    };
558 	    this.doFinalHex = function(hex) {
559 		this.updateHex(hex);
560 		return this.doFinal();
561 	    };
562 	}
563     };
564 
565     /**
566      * update digest by specified string
567      * @name updateString
568      * @memberOf KJUR.crypto.Mac
569      * @function
570      * @param {String} str string to update
571      * @description
572      * @example
573      * md.updateString('New York');
574      */
575     this.updateString = function(str) {
576 	throw "updateString(str) not supported for this alg/prov: " + this.algProv;
577     };
578 
579     /**
580      * update digest by specified hexadecimal string
581      * @name updateHex
582      * @memberOf KJUR.crypto.Mac
583      * @function
584      * @param {String} hex hexadecimal string to update
585      * @description
586      * @example
587      * md.updateHex('0afe36');
588      */
589     this.updateHex = function(hex) {
590 	throw "updateHex(hex) not supported for this alg/prov: " + this.algProv;
591     };
592 
593     /**
594      * completes hash calculation and returns hash result
595      * @name doFinal
596      * @memberOf KJUR.crypto.Mac
597      * @function
598      * @description
599      * @example
600      * md.digest()
601      */
602     this.doFinal = function() {
603 	throw "digest() not supported for this alg/prov: " + this.algProv;
604     };
605 
606     /**
607      * performs final update on the digest using string, then completes the digest computation
608      * @name doFinalString
609      * @memberOf KJUR.crypto.Mac
610      * @function
611      * @param {String} str string to final update
612      * @description
613      * @example
614      * md.digestString('aaa')
615      */
616     this.doFinalString = function(str) {
617 	throw "digestString(str) not supported for this alg/prov: " + this.algProv;
618     };
619 
620     /**
621      * performs final update on the digest using hexadecimal string, 
622      * then completes the digest computation
623      * @name doFinalHex
624      * @memberOf KJUR.crypto.Mac
625      * @function
626      * @param {String} hex hexadecimal string to final update
627      * @description
628      * @example
629      * md.digestHex('0f2abd')
630      */
631     this.doFinalHex = function(hex) {
632 	throw "digestHex(hex) not supported for this alg/prov: " + this.algProv;
633     };
634 
635     /**
636      * set password for Mac
637      * @name setPassword
638      * @memberOf KJUR.crypto.Mac
639      * @function
640      * @param {Object} pass password for Mac
641      * @since crypto 1.1.7 jsrsasign 4.9.0
642      * @description
643      * This method will set password for (H)Mac internally.
644      * Argument 'pass' can be specified as following:
645      * <ul>
646      * <li>even length string of 0..9, a..f or A-F: implicitly specified as hexadecimal string</li>
647      * <li>not above string: implicitly specified as raw string</li>
648      * <li>{rstr: "\x65\x70"}: explicitly specified as raw string</li>
649      * <li>{hex: "6570"}: explicitly specified as hexacedimal string</li>
650      * <li>{utf8: "秘密"}: explicitly specified as UTF8 string</li>
651      * <li>{b64: "Mi78..=="}: explicitly specified as Base64 string</li>
652      * <li>{b64u: "Mi7-_"}: explicitly specified as Base64URL string</li>
653      * </ul>
654      * It is *STRONGLY RECOMMENDED* that explicit representation of password argument
655      * to avoid ambiguity. For example string  "6161" can mean a string "6161" or 
656      * a hexadecimal string of "aa" (i.e. \x61\x61).
657      * @example
658      * mac = KJUR.crypto.Mac({'alg': 'hmacsha256'});
659      * // set password by implicit raw string
660      * mac.setPassword("\x65\x70\xb9\x0b");
661      * mac.setPassword("password");
662      * // set password by implicit hexadecimal string
663      * mac.setPassword("6570b90b");
664      * mac.setPassword("6570B90B");
665      * // set password by explicit raw string
666      * mac.setPassword({"rstr": "\x65\x70\xb9\x0b"});
667      * // set password by explicit hexadecimal string
668      * mac.setPassword({"hex": "6570b90b"});
669      * // set password by explicit utf8 string
670      * mac.setPassword({"utf8": "passwordパスワード");
671      * // set password by explicit Base64 string
672      * mac.setPassword({"b64": "Mb+c3f/=="});
673      * // set password by explicit Base64URL string
674      * mac.setPassword({"b64u": "Mb-c3f_"});
675      */
676     this.setPassword = function(pass) {
677 	// internal this.pass shall be CryptoJS DWord Object for CryptoJS bug
678 	// work around. CrytoJS HMac password can be passed by
679 	// raw string as described in the manual however it doesn't
680 	// work properly in some case. If password was passed
681 	// by CryptoJS DWord which is not described in the manual
682 	// it seems to work. (fixed since crypto 1.1.7)
683 
684 	if (typeof pass == 'string') {
685 	    var hPass = pass;
686 	    if (pass.length % 2 == 1 || ! pass.match(/^[0-9A-Fa-f]+$/)) { // raw str
687 		hPass = rstrtohex(pass);
688 	    }
689 	    this.pass = CryptoJS.enc.Hex.parse(hPass);
690 	    return;
691 	}
692 
693 	if (typeof pass != 'object')
694 	    throw "KJUR.crypto.Mac unsupported password type: " + pass;
695 	
696 	var hPass = null;
697 	if (pass.hex  !== undefined) {
698 	    if (pass.hex.length % 2 != 0 || ! pass.hex.match(/^[0-9A-Fa-f]+$/))
699 		throw "Mac: wrong hex password: " + pass.hex;
700 	    hPass = pass.hex;
701 	}
702 	if (pass.utf8 !== undefined) hPass = utf8tohex(pass.utf8);
703 	if (pass.rstr !== undefined) hPass = rstrtohex(pass.rstr);
704 	if (pass.b64  !== undefined) hPass = b64tohex(pass.b64);
705 	if (pass.b64u !== undefined) hPass = b64utohex(pass.b64u);
706 
707 	if (hPass == null)
708 	    throw "KJUR.crypto.Mac unsupported password type: " + pass;
709 
710 	this.pass = CryptoJS.enc.Hex.parse(hPass);
711     };
712 
713     if (params !== undefined) {
714 	if (params.pass !== undefined) {
715 	    this.setPassword(params.pass);
716 	}
717 	if (params.alg !== undefined) {
718 	    this.algName = params.alg;
719 	    if (params['prov'] === undefined)
720 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
721 	    this.setAlgAndProvider(this.algName, this.provName);
722 	}
723     }
724 };
725 
726 /**
727  * Signature class which is very similar to java.security.Signature class
728  * @name KJUR.crypto.Signature
729  * @class Signature class which is very similar to java.security.Signature class
730  * @param {Array} params parameters for constructor
731  * @property {String} state Current state of this signature object whether 'SIGN', 'VERIFY' or null
732  * @description
733  * <br/>
734  * As for params of constructor's argument, it can be specify following attributes:
735  * <ul>
736  * <li>alg - signature algorithm name (ex. {MD5,SHA1,SHA224,SHA256,SHA384,SHA512,RIPEMD160}with{RSA,ECDSA,DSA})</li>
737  * <li>provider - currently 'cryptojs/jsrsa' only</li>
738  * </ul>
739  * <h4>SUPPORTED ALGORITHMS AND PROVIDERS</h4>
740  * This Signature class supports following signature algorithm and provider names:
741  * <ul>
742  * <li>MD5withRSA - cryptojs/jsrsa</li>
743  * <li>SHA1withRSA - cryptojs/jsrsa</li>
744  * <li>SHA224withRSA - cryptojs/jsrsa</li>
745  * <li>SHA256withRSA - cryptojs/jsrsa</li>
746  * <li>SHA384withRSA - cryptojs/jsrsa</li>
747  * <li>SHA512withRSA - cryptojs/jsrsa</li>
748  * <li>RIPEMD160withRSA - cryptojs/jsrsa</li>
749  * <li>MD5withECDSA - cryptojs/jsrsa</li>
750  * <li>SHA1withECDSA - cryptojs/jsrsa</li>
751  * <li>SHA224withECDSA - cryptojs/jsrsa</li>
752  * <li>SHA256withECDSA - cryptojs/jsrsa</li>
753  * <li>SHA384withECDSA - cryptojs/jsrsa</li>
754  * <li>SHA512withECDSA - cryptojs/jsrsa</li>
755  * <li>RIPEMD160withECDSA - cryptojs/jsrsa</li>
756  * <li>MD5withRSAandMGF1 - cryptojs/jsrsa</li>
757  * <li>SHA1withRSAandMGF1 - cryptojs/jsrsa</li>
758  * <li>SHA224withRSAandMGF1 - cryptojs/jsrsa</li>
759  * <li>SHA256withRSAandMGF1 - cryptojs/jsrsa</li>
760  * <li>SHA384withRSAandMGF1 - cryptojs/jsrsa</li>
761  * <li>SHA512withRSAandMGF1 - cryptojs/jsrsa</li>
762  * <li>RIPEMD160withRSAandMGF1 - cryptojs/jsrsa</li>
763  * <li>SHA1withDSA - cryptojs/jsrsa</li>
764  * <li>SHA224withDSA - cryptojs/jsrsa</li>
765  * <li>SHA256withDSA - cryptojs/jsrsa</li>
766  * </ul>
767  * Here are supported elliptic cryptographic curve names and their aliases for ECDSA:
768  * <ul>
769  * <li>secp256k1</li>
770  * <li>secp256r1, NIST P-256, P-256, prime256v1</li>
771  * <li>secp384r1, NIST P-384, P-384</li>
772  * </ul>
773  * NOTE1: DSA signing algorithm is also supported since crypto 1.1.5.
774  * <h4>EXAMPLES</h4>
775  * @example
776  * // RSA signature generation
777  * var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
778  * sig.init(prvKeyPEM);
779  * sig.updateString('aaa');
780  * var hSigVal = sig.sign();
781  *
782  * // DSA signature validation
783  * var sig2 = new KJUR.crypto.Signature({"alg": "SHA1withDSA"});
784  * sig2.init(certPEM);
785  * sig.updateString('aaa');
786  * var isValid = sig2.verify(hSigVal);
787  * 
788  * // ECDSA signing
789  * var sig = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
790  * sig.init(prvKeyPEM);
791  * sig.updateString('aaa');
792  * var sigValueHex = sig.sign();
793  *
794  * // ECDSA verifying
795  * var sig2 = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
796  * sig.init(certPEM);
797  * sig.updateString('aaa');
798  * var isValid = sig.verify(sigValueHex);
799  */
800 KJUR.crypto.Signature = function(params) {
801     var prvKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for signing
802     var pubKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for verifying
803 
804     var md = null; // KJUR.crypto.MessageDigest object
805     var sig = null;
806     var algName = null;
807     var provName = null;
808     var algProvName = null;
809     var mdAlgName = null;
810     var pubkeyAlgName = null;	// rsa,ecdsa,rsaandmgf1(=rsapss)
811     var state = null;
812     var pssSaltLen = -1;
813     var initParams = null;
814 
815     var sHashHex = null; // hex hash value for hex
816     var hDigestInfo = null;
817     var hPaddedDigestInfo = null;
818     var hSign = null;
819 
820     this._setAlgNames = function() {
821 	if (this.algName.match(/^(.+)with(.+)$/)) {
822 	    this.mdAlgName = RegExp.$1.toLowerCase();
823 	    this.pubkeyAlgName = RegExp.$2.toLowerCase();
824 	}
825     };
826 
827     this._zeroPaddingOfSignature = function(hex, bitLength) {
828 	var s = "";
829 	var nZero = bitLength / 4 - hex.length;
830 	for (var i = 0; i < nZero; i++) {
831 	    s = s + "0";
832 	}
833 	return s + hex;
834     };
835 
836     /**
837      * set signature algorithm and provider
838      * @name setAlgAndProvider
839      * @memberOf KJUR.crypto.Signature
840      * @function
841      * @param {String} alg signature algorithm name
842      * @param {String} prov provider name
843      * @description
844      * @example
845      * md.setAlgAndProvider('SHA1withRSA', 'cryptojs/jsrsa');
846      */
847     this.setAlgAndProvider = function(alg, prov) {
848 	this._setAlgNames();
849 	if (prov != 'cryptojs/jsrsa')
850 	    throw "provider not supported: " + prov;
851 
852 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(this.mdAlgName) != -1) {
853 	    try {
854 		this.md = new KJUR.crypto.MessageDigest({'alg':this.mdAlgName});
855 	    } catch (ex) {
856 		throw "setAlgAndProvider hash alg set fail alg=" +
857                       this.mdAlgName + "/" + ex;
858 	    }
859 
860 	    this.init = function(keyparam, pass) {
861 		var keyObj = null;
862 		try {
863 		    if (pass === undefined) {
864 			keyObj = KEYUTIL.getKey(keyparam);
865 		    } else {
866 			keyObj = KEYUTIL.getKey(keyparam, pass);
867 		    }
868 		} catch (ex) {
869 		    throw "init failed:" + ex;
870 		}
871 
872 		if (keyObj.isPrivate === true) {
873 		    this.prvKey = keyObj;
874 		    this.state = "SIGN";
875 		} else if (keyObj.isPublic === true) {
876 		    this.pubKey = keyObj;
877 		    this.state = "VERIFY";
878 		} else {
879 		    throw "init failed.:" + keyObj;
880 		}
881 	    };
882 
883 	    this.initSign = function(params) {
884 		if (typeof params['ecprvhex'] == 'string' &&
885                     typeof params['eccurvename'] == 'string') {
886 		    this.ecprvhex = params['ecprvhex'];
887 		    this.eccurvename = params['eccurvename'];
888 		} else {
889 		    this.prvKey = params;
890 		}
891 		this.state = "SIGN";
892 	    };
893 
894 	    this.initVerifyByPublicKey = function(params) {
895 		if (typeof params['ecpubhex'] == 'string' &&
896 		    typeof params['eccurvename'] == 'string') {
897 		    this.ecpubhex = params['ecpubhex'];
898 		    this.eccurvename = params['eccurvename'];
899 		} else if (params instanceof KJUR.crypto.ECDSA) {
900 		    this.pubKey = params;
901 		} else if (params instanceof RSAKey) {
902 		    this.pubKey = params;
903 		}
904 		this.state = "VERIFY";
905 	    };
906 
907 	    this.initVerifyByCertificatePEM = function(certPEM) {
908 		var x509 = new X509();
909 		x509.readCertPEM(certPEM);
910 		this.pubKey = x509.subjectPublicKeyRSA;
911 		this.state = "VERIFY";
912 	    };
913 
914 	    this.updateString = function(str) {
915 		this.md.updateString(str);
916 	    };
917 
918 	    this.updateHex = function(hex) {
919 		this.md.updateHex(hex);
920 	    };
921 
922 	    this.sign = function() {
923 		this.sHashHex = this.md.digest();
924 		if (typeof this.ecprvhex != "undefined" &&
925 		    typeof this.eccurvename != "undefined") {
926 		    var ec = new KJUR.crypto.ECDSA({'curve': this.eccurvename});
927 		    this.hSign = ec.signHex(this.sHashHex, this.ecprvhex);
928 		} else if (this.prvKey instanceof RSAKey &&
929 		           this.pubkeyAlgName == "rsaandmgf1") {
930 		    this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex,
931 								    this.mdAlgName,
932 								    this.pssSaltLen);
933 		} else if (this.prvKey instanceof RSAKey &&
934 			   this.pubkeyAlgName == "rsa") {
935 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex,
936 								 this.mdAlgName);
937 		} else if (this.prvKey instanceof KJUR.crypto.ECDSA) {
938 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
939 		} else if (this.prvKey instanceof KJUR.crypto.DSA) {
940 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
941 		} else {
942 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
943 		}
944 		return this.hSign;
945 	    };
946 	    this.signString = function(str) {
947 		this.updateString(str);
948 		return this.sign();
949 	    };
950 	    this.signHex = function(hex) {
951 		this.updateHex(hex);
952 		return this.sign();
953 	    };
954 	    this.verify = function(hSigVal) {
955 	        this.sHashHex = this.md.digest();
956 		if (typeof this.ecpubhex != "undefined" &&
957 		    typeof this.eccurvename != "undefined") {
958 		    var ec = new KJUR.crypto.ECDSA({curve: this.eccurvename});
959 		    return ec.verifyHex(this.sHashHex, hSigVal, this.ecpubhex);
960 		} else if (this.pubKey instanceof RSAKey &&
961 			   this.pubkeyAlgName == "rsaandmgf1") {
962 		    return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, hSigVal, 
963 								this.mdAlgName,
964 								this.pssSaltLen);
965 		} else if (this.pubKey instanceof RSAKey &&
966 			   this.pubkeyAlgName == "rsa") {
967 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
968 		} else if (this.pubKey instanceof KJUR.crypto.ECDSA) {
969 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
970 		} else if (this.pubKey instanceof KJUR.crypto.DSA) {
971 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
972 		} else {
973 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
974 		}
975 	    };
976 	}
977     };
978 
979     /**
980      * Initialize this object for signing or verifying depends on key
981      * @name init
982      * @memberOf KJUR.crypto.Signature
983      * @function
984      * @param {Object} key specifying public or private key as plain/encrypted PKCS#5/8 PEM file, certificate PEM or {@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA} object
985      * @param {String} pass (OPTION) passcode for encrypted private key
986      * @since crypto 1.1.3
987      * @description
988      * This method is very useful initialize method for Signature class since
989      * you just specify key then this method will automatically initialize it
990      * using {@link KEYUTIL.getKey} method.
991      * As for 'key',  following argument type are supported:
992      * <h5>signing</h5>
993      * <ul>
994      * <li>PEM formatted PKCS#8 encrypted RSA/ECDSA private key concluding "BEGIN ENCRYPTED PRIVATE KEY"</li>
995      * <li>PEM formatted PKCS#5 encrypted RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" and ",ENCRYPTED"</li>
996      * <li>PEM formatted PKCS#8 plain RSA/ECDSA private key concluding "BEGIN PRIVATE KEY"</li>
997      * <li>PEM formatted PKCS#5 plain RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" without ",ENCRYPTED"</li>
998      * <li>RSAKey object of private key</li>
999      * <li>KJUR.crypto.ECDSA object of private key</li>
1000      * <li>KJUR.crypto.DSA object of private key</li>
1001      * </ul>
1002      * <h5>verification</h5>
1003      * <ul>
1004      * <li>PEM formatted PKCS#8 RSA/EC/DSA public key concluding "BEGIN PUBLIC KEY"</li>
1005      * <li>PEM formatted X.509 certificate with RSA/EC/DSA public key concluding
1006      *     "BEGIN CERTIFICATE", "BEGIN X509 CERTIFICATE" or "BEGIN TRUSTED CERTIFICATE".</li>
1007      * <li>RSAKey object of public key</li>
1008      * <li>KJUR.crypto.ECDSA object of public key</li>
1009      * <li>KJUR.crypto.DSA object of public key</li>
1010      * </ul>
1011      * @example
1012      * sig.init(sCertPEM)
1013      */
1014     this.init = function(key, pass) {
1015 	throw "init(key, pass) not supported for this alg:prov=" +
1016 	      this.algProvName;
1017     };
1018 
1019     /**
1020      * Initialize this object for verifying with a public key
1021      * @name initVerifyByPublicKey
1022      * @memberOf KJUR.crypto.Signature
1023      * @function
1024      * @param {Object} param RSAKey object of public key or associative array for ECDSA
1025      * @since 1.0.2
1026      * @deprecated from crypto 1.1.5. please use init() method instead.
1027      * @description
1028      * Public key information will be provided as 'param' parameter and the value will be
1029      * following:
1030      * <ul>
1031      * <li>{@link RSAKey} object for RSA verification</li>
1032      * <li>associative array for ECDSA verification
1033      *     (ex. <code>{'ecpubhex': '041f..', 'eccurvename': 'secp256r1'}</code>)
1034      * </li>
1035      * </ul>
1036      * @example
1037      * sig.initVerifyByPublicKey(rsaPrvKey)
1038      */
1039     this.initVerifyByPublicKey = function(rsaPubKey) {
1040 	throw "initVerifyByPublicKey(rsaPubKeyy) not supported for this alg:prov=" +
1041 	      this.algProvName;
1042     };
1043 
1044     /**
1045      * Initialize this object for verifying with a certficate
1046      * @name initVerifyByCertificatePEM
1047      * @memberOf KJUR.crypto.Signature
1048      * @function
1049      * @param {String} certPEM PEM formatted string of certificate
1050      * @since 1.0.2
1051      * @deprecated from crypto 1.1.5. please use init() method instead.
1052      * @description
1053      * @example
1054      * sig.initVerifyByCertificatePEM(certPEM)
1055      */
1056     this.initVerifyByCertificatePEM = function(certPEM) {
1057 	throw "initVerifyByCertificatePEM(certPEM) not supported for this alg:prov=" +
1058 	    this.algProvName;
1059     };
1060 
1061     /**
1062      * Initialize this object for signing
1063      * @name initSign
1064      * @memberOf KJUR.crypto.Signature
1065      * @function
1066      * @param {Object} param RSAKey object of public key or associative array for ECDSA
1067      * @deprecated from crypto 1.1.5. please use init() method instead.
1068      * @description
1069      * Private key information will be provided as 'param' parameter and the value will be
1070      * following:
1071      * <ul>
1072      * <li>{@link RSAKey} object for RSA signing</li>
1073      * <li>associative array for ECDSA signing
1074      *     (ex. <code>{'ecprvhex': '1d3f..', 'eccurvename': 'secp256r1'}</code>)</li>
1075      * </ul>
1076      * @example
1077      * sig.initSign(prvKey)
1078      */
1079     this.initSign = function(prvKey) {
1080 	throw "initSign(prvKey) not supported for this alg:prov=" + this.algProvName;
1081     };
1082 
1083     /**
1084      * Updates the data to be signed or verified by a string
1085      * @name updateString
1086      * @memberOf KJUR.crypto.Signature
1087      * @function
1088      * @param {String} str string to use for the update
1089      * @description
1090      * @example
1091      * sig.updateString('aaa')
1092      */
1093     this.updateString = function(str) {
1094 	throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
1095     };
1096 
1097     /**
1098      * Updates the data to be signed or verified by a hexadecimal string
1099      * @name updateHex
1100      * @memberOf KJUR.crypto.Signature
1101      * @function
1102      * @param {String} hex hexadecimal string to use for the update
1103      * @description
1104      * @example
1105      * sig.updateHex('1f2f3f')
1106      */
1107     this.updateHex = function(hex) {
1108 	throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
1109     };
1110 
1111     /**
1112      * Returns the signature bytes of all data updates as a hexadecimal string
1113      * @name sign
1114      * @memberOf KJUR.crypto.Signature
1115      * @function
1116      * @return the signature bytes as a hexadecimal string
1117      * @description
1118      * @example
1119      * var hSigValue = sig.sign()
1120      */
1121     this.sign = function() {
1122 	throw "sign() not supported for this alg:prov=" + this.algProvName;
1123     };
1124 
1125     /**
1126      * performs final update on the sign using string, then returns the signature bytes of all data updates as a hexadecimal string
1127      * @name signString
1128      * @memberOf KJUR.crypto.Signature
1129      * @function
1130      * @param {String} str string to final update
1131      * @return the signature bytes of a hexadecimal string
1132      * @description
1133      * @example
1134      * var hSigValue = sig.signString('aaa')
1135      */
1136     this.signString = function(str) {
1137 	throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
1138     };
1139 
1140     /**
1141      * performs final update on the sign using hexadecimal string, then returns the signature bytes of all data updates as a hexadecimal string
1142      * @name signHex
1143      * @memberOf KJUR.crypto.Signature
1144      * @function
1145      * @param {String} hex hexadecimal string to final update
1146      * @return the signature bytes of a hexadecimal string
1147      * @description
1148      * @example
1149      * var hSigValue = sig.signHex('1fdc33')
1150      */
1151     this.signHex = function(hex) {
1152 	throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
1153     };
1154 
1155     /**
1156      * verifies the passed-in signature.
1157      * @name verify
1158      * @memberOf KJUR.crypto.Signature
1159      * @function
1160      * @param {String} str string to final update
1161      * @return {Boolean} true if the signature was verified, otherwise false
1162      * @description
1163      * @example
1164      * var isValid = sig.verify('1fbcefdca4823a7(snip)')
1165      */
1166     this.verify = function(hSigVal) {
1167 	throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
1168     };
1169 
1170     this.initParams = params;
1171 
1172     if (params !== undefined) {
1173 	if (params['alg'] !== undefined) {
1174 	    this.algName = params['alg'];
1175 	    if (params['prov'] === undefined) {
1176 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
1177 	    } else {
1178 		this.provName = params['prov'];
1179 	    }
1180 	    this.algProvName = this.algName + ":" + this.provName;
1181 	    this.setAlgAndProvider(this.algName, this.provName);
1182 	    this._setAlgNames();
1183 	}
1184 
1185 	if (params['psssaltlen'] !== undefined) this.pssSaltLen = params['psssaltlen'];
1186 
1187 	if (params['prvkeypem'] !== undefined) {
1188 	    if (params['prvkeypas'] !== undefined) {
1189 		throw "both prvkeypem and prvkeypas parameters not supported";
1190 	    } else {
1191 		try {
1192 		    var prvKey = new RSAKey();
1193 		    prvKey.readPrivateKeyFromPEMString(params['prvkeypem']);
1194 		    this.initSign(prvKey);
1195 		} catch (ex) {
1196 		    throw "fatal error to load pem private key: " + ex;
1197 		}
1198 	    }
1199 	}
1200     }
1201 };
1202 
1203 /**
1204  * static object for cryptographic function utilities
1205  * @name KJUR.crypto.OID
1206  * @class static object for cryptography related OIDs
1207  * @property {Array} oidhex2name key value of hexadecimal OID and its name
1208  *           (ex. '2a8648ce3d030107' and 'secp256r1')
1209  * @since crypto 1.1.3
1210  * @description
1211  */
1212 
1213 
1214 KJUR.crypto.OID = new function() {
1215     this.oidhex2name = {
1216 	'2a864886f70d010101': 'rsaEncryption',
1217 	'2a8648ce3d0201': 'ecPublicKey',
1218 	'2a8648ce380401': 'dsa',
1219 	'2a8648ce3d030107': 'secp256r1',
1220 	'2b8104001f': 'secp192k1',
1221 	'2b81040021': 'secp224r1',
1222 	'2b8104000a': 'secp256k1',
1223 	'2b81040023': 'secp521r1',
1224 	'2b81040022': 'secp384r1',
1225 	'2a8648ce380403': 'SHA1withDSA', // 1.2.840.10040.4.3
1226 	'608648016503040301': 'SHA224withDSA', // 2.16.840.1.101.3.4.3.1
1227 	'608648016503040302': 'SHA256withDSA', // 2.16.840.1.101.3.4.3.2
1228     };
1229 };
1230