This commit is contained in:
Patrick Roumanoff 2015-09-04 17:13:08 +10:00
parent 6ff1e44602
commit e4a0c7f193
4 changed files with 77 additions and 60 deletions

View file

@ -1,9 +1,10 @@
/*jslint browser: true, sloppy: true */
//adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-C
function base64urlEncode(s) {
function base64urlEncode(arg) {
var s = window.btoa(arg); // Regular base64 encoder
s = s.split('=')[0]; // Remove any trailing '='s
s = s.replace(/+/g, '-'); // 62nd char of encoding
s = s.replace(/\+/g, '-'); // 62nd char of encoding
s = s.replace(/\//g, '_'); // 63rd char of encoding
return s;
}
@ -12,10 +13,16 @@ function base64urlDecode(s) {
s = s.replace(/-/g, '+'); // 62nd char of encoding
s = s.replace(/_/g, '/'); // 63rd char of encoding
switch (s.length % 4) { // Pad with trailing '='s
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
default: throw "Illegal base64url string!";
case 0: // No pad chars in this case
break;
case 2: // Two pad chars
s += "==";
break;
case 3: // One pad char
s += "=";
break;
default:
throw "Illegal base64url string!";
}
return window.atob(s); // Standard base64 decoder
}

View file

@ -1,3 +1,6 @@
/*jslint browser: true, sloppy: true, vars: true, indent: 2*/
var console, generateKeyPair;
function copy(id) {
return function () {
var ta = document.querySelector(id);
@ -12,7 +15,7 @@ function copy(id) {
}
window.getSelection().removeAllRanges();
ta.blur();
}
};
}
function buildHref(data) {
@ -35,7 +38,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
document.querySelector('a#public').setAttribute("download", name + "_rsa.pub");
var alg = document.querySelector('#alg').value || "RSASSA-PKCS1-v1_5";
var size = parseInt(document.querySelector('#size').value || "2048");
var size = parseInt(document.querySelector('#size').value || "2048", 10);
generateKeyPair(alg, size, name).then(function (keys) {
document.querySelector('#private').setAttribute("href", buildHref(keys[0]));
document.querySelector('#public').setAttribute("href", buildHref(keys[1]));

View file

@ -1,4 +1,7 @@
/*jslint browser: true, devel: true, sloppy: true, vars: true*/
/*globals Uint8Array, Promise */
var extractable = true;
var encodePrivateKey, encodePublicKey;
function wrap(text, len) {
var length = len || 72, i, result = "";
@ -13,10 +16,10 @@ function rsaPrivateKey(key) {
}
function arrayBufferToBase64(buffer) {
var binary = '';
var binary = '', i;
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
for (i = 0; i < len; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
@ -27,24 +30,24 @@ function generateKeyPair(alg, size, name) {
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048, //can be 1024, 2048, or 4096
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-1"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
hash: {name: "SHA-1"} //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
extractable,
["sign", "verify"]
).then(function (key) {
var private = window.crypto.subtle.exportKey(
var privateKey = window.crypto.subtle.exportKey(
"jwk",
key.privateKey
).then(encodePrivateKey).then(wrap).then(rsaPrivateKey);
var public = window.crypto.subtle.exportKey(
var publicKey = window.crypto.subtle.exportKey(
"jwk",
key.publicKey
).then(function (jwk) {
return encodePublicKey(jwk, name);
});
return Promise.all([private, public]);
return Promise.all([privateKey, publicKey]);
});
}

View file

@ -1,3 +1,7 @@
/*jslint browser: true, devel: true, bitwise: true, sloppy: true, vars: true*/
var base64urlDecode;
function arrayToString(a) {
return String.fromCharCode.apply(null, a);
}
@ -24,7 +28,7 @@ function arrayToPem(a) {
function arrayToLen(a) {
var result = 0, i;
for(i = 0; i < a.length; i++) {
for (i = 0; i < a.length; i += 1) {
result = result * 256 + a[i];
}
return result;
@ -32,7 +36,7 @@ function arrayToLen(a) {
function integerToOctet(n) {
var result = [];
for(;n > 0; n = n >> 8 ) {
for (true; n > 0; n = n >> 8) {
result.push(n & 0xFF);
}
return result.reverse();
@ -40,7 +44,7 @@ function integerToOctet(n) {
function lenToArray(n) {
var oct = integerToOctet(n), i;
for(i = oct.length; i < 4; i++) {
for (i = oct.length; i < 4; i += 1) {
oct.unshift(0);
}
return oct;
@ -49,13 +53,13 @@ function lenToArray(n) {
function decodePublicKey(s) {
var split = s.split(" ");
var prefix = split[0];
if(prefix != "ssh-rsa") {
if (prefix !== "ssh-rsa") {
throw ("Unknown prefix:" + prefix);
}
var buffer = pemToArray(split[1]);
var nameLen = arrayToLen(buffer.splice(0, 4));
var type = arrayToString(buffer.splice(0, nameLen));
if(type != "ssh-rsa") {
if (type !== "ssh-rsa") {
throw ("Unknown key type:" + type);
}
var exponentLen = arrayToLen(buffer.splice(0, 4));
@ -65,6 +69,13 @@ function decodePublicKey(s) {
return {type: type, exponent: exponent, key: key, name: split[2]};
}
function checkHighestBit(v) {
if (v[0] >> 7 === 1) { // add leading zero if first bit is set
v.unshift(0);
}
return v;
}
function jwkToInternal(jwk) {
return {
type: "ssh-rsa",
@ -74,13 +85,6 @@ function jwkToInternal(jwk) {
};
}
function checkHighestBit(v) {
if(v[0] >> 7 === 1) { // add leading zero if first bit is set
v.unshift(0);
}
return v;
}
function encodePublicKey(jwk, name) {
var k = jwkToInternal(jwk);
k.name = name;