jslint
This commit is contained in:
parent
6ff1e44602
commit
e4a0c7f193
4 changed files with 77 additions and 60 deletions
19
base64url.js
19
base64url.js
|
@ -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
|
//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
|
var s = window.btoa(arg); // Regular base64 encoder
|
||||||
s = s.split('=')[0]; // Remove any trailing '='s
|
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
|
s = s.replace(/\//g, '_'); // 63rd char of encoding
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -12,10 +13,16 @@ function base64urlDecode(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
|
s = s.replace(/_/g, '/'); // 63rd char of encoding
|
||||||
switch (s.length % 4) { // Pad with trailing '='s
|
switch (s.length % 4) { // Pad with trailing '='s
|
||||||
case 0: break; // No pad chars in this case
|
case 0: // No pad chars in this case
|
||||||
case 2: s += "=="; break; // Two pad chars
|
break;
|
||||||
case 3: s += "="; break; // One pad char
|
case 2: // Two pad chars
|
||||||
default: throw "Illegal base64url string!";
|
s += "==";
|
||||||
|
break;
|
||||||
|
case 3: // One pad char
|
||||||
|
s += "=";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw "Illegal base64url string!";
|
||||||
}
|
}
|
||||||
return window.atob(s); // Standard base64 decoder
|
return window.atob(s); // Standard base64 decoder
|
||||||
}
|
}
|
|
@ -1,3 +1,6 @@
|
||||||
|
/*jslint browser: true, sloppy: true, vars: true, indent: 2*/
|
||||||
|
var console, generateKeyPair;
|
||||||
|
|
||||||
function copy(id) {
|
function copy(id) {
|
||||||
return function () {
|
return function () {
|
||||||
var ta = document.querySelector(id);
|
var ta = document.querySelector(id);
|
||||||
|
@ -12,7 +15,7 @@ function copy(id) {
|
||||||
}
|
}
|
||||||
window.getSelection().removeAllRanges();
|
window.getSelection().removeAllRanges();
|
||||||
ta.blur();
|
ta.blur();
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildHref(data) {
|
function buildHref(data) {
|
||||||
|
@ -35,7 +38,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
|
||||||
document.querySelector('a#public').setAttribute("download", name + "_rsa.pub");
|
document.querySelector('a#public').setAttribute("download", name + "_rsa.pub");
|
||||||
|
|
||||||
var alg = document.querySelector('#alg').value || "RSASSA-PKCS1-v1_5";
|
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) {
|
generateKeyPair(alg, size, name).then(function (keys) {
|
||||||
document.querySelector('#private').setAttribute("href", buildHref(keys[0]));
|
document.querySelector('#private').setAttribute("href", buildHref(keys[0]));
|
||||||
document.querySelector('#public').setAttribute("href", buildHref(keys[1]));
|
document.querySelector('#public').setAttribute("href", buildHref(keys[1]));
|
||||||
|
|
15
js-keygen.js
15
js-keygen.js
|
@ -1,4 +1,7 @@
|
||||||
|
/*jslint browser: true, devel: true, sloppy: true, vars: true*/
|
||||||
|
/*globals Uint8Array, Promise */
|
||||||
var extractable = true;
|
var extractable = true;
|
||||||
|
var encodePrivateKey, encodePublicKey;
|
||||||
|
|
||||||
function wrap(text, len) {
|
function wrap(text, len) {
|
||||||
var length = len || 72, i, result = "";
|
var length = len || 72, i, result = "";
|
||||||
|
@ -13,10 +16,10 @@ function rsaPrivateKey(key) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferToBase64(buffer) {
|
function arrayBufferToBase64(buffer) {
|
||||||
var binary = '';
|
var binary = '', i;
|
||||||
var bytes = new Uint8Array(buffer);
|
var bytes = new Uint8Array(buffer);
|
||||||
var len = bytes.byteLength;
|
var len = bytes.byteLength;
|
||||||
for (var i = 0; i < len; i++) {
|
for (i = 0; i < len; i += 1) {
|
||||||
binary += String.fromCharCode(bytes[i]);
|
binary += String.fromCharCode(bytes[i]);
|
||||||
}
|
}
|
||||||
return window.btoa(binary);
|
return window.btoa(binary);
|
||||||
|
@ -27,24 +30,24 @@ function generateKeyPair(alg, size, name) {
|
||||||
name: "RSASSA-PKCS1-v1_5",
|
name: "RSASSA-PKCS1-v1_5",
|
||||||
modulusLength: 2048, //can be 1024, 2048, or 4096
|
modulusLength: 2048, //can be 1024, 2048, or 4096
|
||||||
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
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,
|
extractable,
|
||||||
["sign", "verify"]
|
["sign", "verify"]
|
||||||
).then(function (key) {
|
).then(function (key) {
|
||||||
|
|
||||||
var private = window.crypto.subtle.exportKey(
|
var privateKey = window.crypto.subtle.exportKey(
|
||||||
"jwk",
|
"jwk",
|
||||||
key.privateKey
|
key.privateKey
|
||||||
).then(encodePrivateKey).then(wrap).then(rsaPrivateKey);
|
).then(encodePrivateKey).then(wrap).then(rsaPrivateKey);
|
||||||
|
|
||||||
var public = window.crypto.subtle.exportKey(
|
var publicKey = window.crypto.subtle.exportKey(
|
||||||
"jwk",
|
"jwk",
|
||||||
key.publicKey
|
key.publicKey
|
||||||
).then(function (jwk) {
|
).then(function (jwk) {
|
||||||
return encodePublicKey(jwk, name);
|
return encodePublicKey(jwk, name);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all([private, public]);
|
return Promise.all([privateKey, publicKey]);
|
||||||
});
|
});
|
||||||
}
|
}
|
28
ssh-util.js
28
ssh-util.js
|
@ -1,3 +1,7 @@
|
||||||
|
/*jslint browser: true, devel: true, bitwise: true, sloppy: true, vars: true*/
|
||||||
|
|
||||||
|
var base64urlDecode;
|
||||||
|
|
||||||
function arrayToString(a) {
|
function arrayToString(a) {
|
||||||
return String.fromCharCode.apply(null, a);
|
return String.fromCharCode.apply(null, a);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +28,7 @@ function arrayToPem(a) {
|
||||||
|
|
||||||
function arrayToLen(a) {
|
function arrayToLen(a) {
|
||||||
var result = 0, i;
|
var result = 0, i;
|
||||||
for(i = 0; i < a.length; i++) {
|
for (i = 0; i < a.length; i += 1) {
|
||||||
result = result * 256 + a[i];
|
result = result * 256 + a[i];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -32,7 +36,7 @@ function arrayToLen(a) {
|
||||||
|
|
||||||
function integerToOctet(n) {
|
function integerToOctet(n) {
|
||||||
var result = [];
|
var result = [];
|
||||||
for(;n > 0; n = n >> 8 ) {
|
for (true; n > 0; n = n >> 8) {
|
||||||
result.push(n & 0xFF);
|
result.push(n & 0xFF);
|
||||||
}
|
}
|
||||||
return result.reverse();
|
return result.reverse();
|
||||||
|
@ -40,7 +44,7 @@ function integerToOctet(n) {
|
||||||
|
|
||||||
function lenToArray(n) {
|
function lenToArray(n) {
|
||||||
var oct = integerToOctet(n), i;
|
var oct = integerToOctet(n), i;
|
||||||
for(i = oct.length; i < 4; i++) {
|
for (i = oct.length; i < 4; i += 1) {
|
||||||
oct.unshift(0);
|
oct.unshift(0);
|
||||||
}
|
}
|
||||||
return oct;
|
return oct;
|
||||||
|
@ -49,13 +53,13 @@ function lenToArray(n) {
|
||||||
function decodePublicKey(s) {
|
function decodePublicKey(s) {
|
||||||
var split = s.split(" ");
|
var split = s.split(" ");
|
||||||
var prefix = split[0];
|
var prefix = split[0];
|
||||||
if(prefix != "ssh-rsa") {
|
if (prefix !== "ssh-rsa") {
|
||||||
throw ("Unknown prefix:" + prefix);
|
throw ("Unknown prefix:" + prefix);
|
||||||
}
|
}
|
||||||
var buffer = pemToArray(split[1]);
|
var buffer = pemToArray(split[1]);
|
||||||
var nameLen = arrayToLen(buffer.splice(0, 4));
|
var nameLen = arrayToLen(buffer.splice(0, 4));
|
||||||
var type = arrayToString(buffer.splice(0, nameLen));
|
var type = arrayToString(buffer.splice(0, nameLen));
|
||||||
if(type != "ssh-rsa") {
|
if (type !== "ssh-rsa") {
|
||||||
throw ("Unknown key type:" + type);
|
throw ("Unknown key type:" + type);
|
||||||
}
|
}
|
||||||
var exponentLen = arrayToLen(buffer.splice(0, 4));
|
var exponentLen = arrayToLen(buffer.splice(0, 4));
|
||||||
|
@ -65,6 +69,13 @@ function decodePublicKey(s) {
|
||||||
return {type: type, exponent: exponent, key: key, name: split[2]};
|
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) {
|
function jwkToInternal(jwk) {
|
||||||
return {
|
return {
|
||||||
type: "ssh-rsa",
|
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) {
|
function encodePublicKey(jwk, name) {
|
||||||
var k = jwkToInternal(jwk);
|
var k = jwkToInternal(jwk);
|
||||||
k.name = name;
|
k.name = name;
|
||||||
|
|
Loading…
Reference in a new issue