AJ ONeal
5 år sedan
7 ändrade filer med 196 tillägg och 80 borttagningar
@ -0,0 +1,2 @@ |
|||
'use strict'; |
|||
module.exports = require('./lib/rasha.js'); |
@ -0,0 +1,56 @@ |
|||
'use strict'; |
|||
|
|||
var Enc = module.exports; |
|||
|
|||
Enc.bufToHex = function toHex(u8) { |
|||
var hex = []; |
|||
var i, h; |
|||
|
|||
for (i = 0; i < u8.byteLength; i += 1) { |
|||
h = u8[i].toString(16); |
|||
if (2 !== h.length) { h = '0' + h; } |
|||
hex.push(h); |
|||
} |
|||
|
|||
return hex.join('').toLowerCase(); |
|||
}; |
|||
|
|||
/* |
|||
Enc.strToBin = function strToBin(str) { |
|||
var escstr = encodeURIComponent(str); |
|||
// replaces any uri escape sequence, such as %0A,
|
|||
// with binary escape, such as 0x0A
|
|||
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) { |
|||
return String.fromCharCode(parseInt(p1, 16)); |
|||
}); |
|||
|
|||
return binstr; |
|||
}; |
|||
*/ |
|||
|
|||
/* |
|||
Enc.strToBase64 = function strToBase64(str) { |
|||
// node automatically can tell the difference
|
|||
// between uc2 (utf-8) strings and binary strings
|
|||
// so we don't have to re-encode the strings
|
|||
return Buffer.from(str).toString('base64'); |
|||
}; |
|||
*/ |
|||
|
|||
/* |
|||
Enc.urlBase64ToBase64 = function urlsafeBase64ToBase64(str) { |
|||
var r = str % 4; |
|||
if (2 === r) { |
|||
str += '=='; |
|||
} else if (3 === r) { |
|||
str += '='; |
|||
} |
|||
return str.replace(/-/g, '+').replace(/_/g, '/'); |
|||
}; |
|||
*/ |
|||
|
|||
Enc.base64ToBuf = function base64ToBuf(str) { |
|||
// always convert from urlsafe base64, just in case
|
|||
//return Buffer.from(Enc.urlBase64ToBase64(str)).toString('base64');
|
|||
return Buffer.from(str, 'base64'); |
|||
}; |
@ -0,0 +1,39 @@ |
|||
'use strict'; |
|||
|
|||
var PEM = module.exports; |
|||
var Enc = require('./encoding.js'); |
|||
|
|||
PEM.RSA_OBJID = '06 09 2A864886F70D010101' |
|||
.replace(/\s+/g, '').toLowerCase(); |
|||
|
|||
PEM.parseBlock = function pemToDer(pem) { |
|||
var typ; |
|||
var pub; |
|||
var hex; |
|||
var der = Enc.base64ToBuf(pem.split(/\n/).filter(function (line, i) { |
|||
if (0 === i) { |
|||
if (/ PUBLIC /.test(line)) { |
|||
pub = true; |
|||
} else if (/ PRIVATE /.test(line)) { |
|||
pub = false; |
|||
} |
|||
if (/ RSA /.test(line)) { |
|||
typ = 'RSA'; |
|||
} |
|||
} |
|||
return !/---/.test(line); |
|||
}).join('')); |
|||
|
|||
if (!typ) { |
|||
hex = Enc.bufToHex(der); |
|||
if (-1 !== hex.indexOf(PEM.RSA_OBJID)) { |
|||
typ = 'RSA'; |
|||
} |
|||
} |
|||
if (!typ) { |
|||
console.warn("Definitely not an RSA PKCS#8 because there's no RSA Object ID in the DER body."); |
|||
console.warn("Probably not an RSA PKCS#1 because 'RSA' wasn't in the PEM type string."); |
|||
} |
|||
|
|||
return { kty: typ, pub: pub, der: der }; |
|||
}; |
@ -1,3 +1,59 @@ |
|||
'use strict'; |
|||
|
|||
var RSA = module.exports; |
|||
var ASN1 = require('./asn1.js'); |
|||
//var Enc = require('./encoding.js');
|
|||
var PEM = require('./pem.js'); |
|||
var SSH = require('./ssh.js'); |
|||
|
|||
|
|||
/* |
|||
RSAPrivateKey ::= SEQUENCE { |
|||
version Version, |
|||
modulus INTEGER, -- n |
|||
publicExponent INTEGER, -- e |
|||
privateExponent INTEGER, -- d |
|||
prime1 INTEGER, -- p |
|||
prime2 INTEGER, -- q |
|||
exponent1 INTEGER, -- d mod (p-1) |
|||
exponent2 INTEGER, -- d mod (q-1) |
|||
coefficient INTEGER, -- (inverse of q) mod p |
|||
otherPrimeInfos OtherPrimeInfos OPTIONAL |
|||
} |
|||
*/ |
|||
|
|||
/*global Promise*/ |
|||
RSA.parse = function parseEc(opts) { |
|||
return Promise.resolve().then(function () { |
|||
if (!opts || !opts.pem || 'string' !== typeof opts.pem) { |
|||
throw new Error("must pass { pem: pem } as a string"); |
|||
} |
|||
if (0 === opts.pem.indexOf('ssh-rsa ')) { |
|||
return SSH.parse(opts.pem); |
|||
} |
|||
var pem = opts.pem; |
|||
var block = PEM.parseBlock(pem); |
|||
//var hex = toHex(u8);
|
|||
//var jwk = { kty: 'RSA' };
|
|||
|
|||
var asn1 = ASN1.parse(block.der); |
|||
var ws = ''; |
|||
function write(asn1) { |
|||
console.log(ws, 'ch', Buffer.from([asn1.type]).toString('hex'), asn1.length); |
|||
if (!asn1.children) { |
|||
return; |
|||
} |
|||
asn1.children.forEach(function (a) { |
|||
ws += '\t'; |
|||
write(a); |
|||
ws = ws.slice(1); |
|||
}); |
|||
} |
|||
//console.log(JSON.stringify(asn1, null, 2));
|
|||
console.log(asn1); |
|||
write(asn1); |
|||
|
|||
return { kty: 'RSA' }; |
|||
}); |
|||
}; |
|||
RSA.toJwk = RSA.import = RSA.parse; |
|||
|
@ -0,0 +1,10 @@ |
|||
'use strict'; |
|||
|
|||
var SSH = module.exports; |
|||
|
|||
// 7 s s h - r s a
|
|||
SSH.RSA = '00000007 73 73 68 2d 72 73 61'.replace(/\s+/g, '').toLowerCase(); |
|||
|
|||
SSH.parse = function (pem) { |
|||
|
|||
}; |
Laddar…
Reference in new issue