aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/rsa-pem.js
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2011-05-19 02:31:04 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2011-05-19 02:31:04 -0400
commitaf055136c23652f1cf1ef0beac1b94d79cc1ddb6 (patch)
treec4de7f3bfe91303423e19552f850d432b03133c6 /crypto/rsa-pem.js
parent53fb8167efe4100bf20df3bec5d6b844f4bf1617 (diff)
downloadalias-af055136c23652f1cf1ef0beac1b94d79cc1ddb6.tar.gz
Started to play around with js crypto code
test.js implements the basic functionality (recommand to load it in v8 (d8 test.js --shell)
Diffstat (limited to 'crypto/rsa-pem.js')
-rw-r--r--crypto/rsa-pem.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/crypto/rsa-pem.js b/crypto/rsa-pem.js
new file mode 100644
index 0000000..8236068
--- /dev/null
+++ b/crypto/rsa-pem.js
@@ -0,0 +1,71 @@
+//
+// rsa-pem.js - adding function for reading/writing PKCS#1 PEM private key
+// to RSAKey class.
+//
+// version: 1.0 (2010-Jun-03)
+//
+// Copyright (c) 2010 Kenji Urushima (kenji.urushima@gmail.com)
+//
+// This software is licensed under the terms of the MIT License.
+// http://www.opensource.org/licenses/mit-license.php
+//
+// The above copyright and license notice shall be
+// included in all copies or substantial portions of the Software.
+//
+//
+// Depends on:
+//
+//
+//
+// _RSApem_pemToBase64(sPEM)
+//
+// removing PEM header, PEM footer and space characters including
+// new lines from PEM formatted RSA private key string.
+//
+function _rsapem_pemToBase64(sPEMPrivateKey) {
+ var s = sPEMPrivateKey;
+ s = s.replace("-----BEGIN RSA PRIVATE KEY-----", "");
+ s = s.replace("-----END RSA PRIVATE KEY-----", "");
+ s = s.replace(/[ \n]+/g, "");
+ return s;
+}
+
+function _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey) {
+ var a = new Array();
+ var v1 = _asnhex_getStartPosOfV_AtObj(hPrivateKey, 0);
+ var n1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, v1);
+ var e1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, n1);
+ var d1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, e1);
+ var p1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, d1);
+ var q1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, p1);
+ var dp1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, q1);
+ var dq1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, dp1);
+ var co1 = _asnhex_getPosOfNextSibling_AtObj(hPrivateKey, dq1);
+ a.push(v1, n1, e1, d1, p1, q1, dp1, dq1, co1);
+ return a;
+}
+
+function _rsapem_getHexValueArrayOfChildrenFromHex(hPrivateKey) {
+ var posArray = _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey);
+ var v = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[0]);
+ var n = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[1]);
+ var e = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[2]);
+ var d = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[3]);
+ var p = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[4]);
+ var q = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[5]);
+ var dp = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[6]);
+ var dq = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[7]);
+ var co = _asnhex_getHexOfV_AtObj(hPrivateKey, posArray[8]);
+ var a = new Array();
+ a.push(v, n, e, d, p, q, dp, dq, co);
+ return a;
+}
+
+function _rsapem_readPrivateKeyFromPEMString(keyPEM) {
+ var keyB64 = _rsapem_pemToBase64(keyPEM);
+ var keyHex = b64tohex(keyB64) // depends base64.js
+ var a = _rsapem_getHexValueArrayOfChildrenFromHex(keyHex);
+ this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
+}
+
+RSAKey.prototype.readPrivateKeyFromPEMString = _rsapem_readPrivateKeyFromPEMString;