Last active
August 29, 2015 14:14
-
-
Save Ry-Nomad/b174ec37b234fbd3c315 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Object DetectSubtleCrypto() | |
//Establish the availability of various crypto.subtle API's necessary for keygen, signing, verification, hashing | |
function DetectSubtleCrypto(){ | |
var supportedApis = {}; | |
var baselineSupport = ( | |
(crypto && crypto.subtle) | |
&& ( | |
(location.protocol === "https:" || "chrome-extension:" || "chrome:") | |
|| (location.hostname === "localhost" || "127.0.0.1") | |
) | |
) ? true : false ; | |
if (!baselineSupport) { | |
supportedApis = {} | |
} else { | |
try { | |
crypto.subtle.generateKey( | |
{ name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, hash:{name:"SHA-256"}, publicExponent: new Uint8Array([0x01, 0x00, 0x01]) }, | |
true, //exportable; | |
["sign", "verify"]).then(function(result){ | |
if (result.publicKey && result.privateKey) { | |
supportedApis.generateRSASSAKey = true; | |
try{ | |
crypto.subtle.exportKey("pkcs8",key.privateKey).then(function(result){ | |
supportedApis.exportPrivatePKCS8 = true; | |
}); | |
} catch (e) { | |
supportedApis.exportPrivatePKCS8 = false; | |
} | |
try { | |
crypto.subtle.exportKey("raw",key.privateKey).then(function(result){ | |
supportedApis.exportPrivateRAW = true; | |
}); | |
} catch (e) { | |
supportedApis.exportPrivateRAW = false; | |
} | |
try { | |
crypto.subtle.exportKey("jwk",key.privateKey).then(function(result){ | |
supportedApis.exportPrivateJWK = true; | |
}) | |
} catch (e) { | |
supportedApis.exportPrivateJWK = false; | |
} | |
} else { | |
supportedApis.generateRSASSAKey = false | |
} | |
}); | |
} catch (e){ | |
supportedApis.sign = false; | |
supportedApis.verify = false; | |
} | |
var testDigest = new Uint8Array(1000) | |
try{ | |
crypto.subtle.digest({name:"SHA-256"}, testDigest.buffer).then(function(result){\ | |
supportedApis.digestSHA256 = true; | |
}); | |
} catch (e) { | |
supportedAPIs.digestSHA256 = false | |
} | |
} | |
return function (desiredApisArray){ | |
//what I'm thinking here is that KeyChain could supply desired api's based on default/developer needs for encrypt/decrypt, key formats, storage formats, etc. please advise. | |
//The most important thing about returning this closure is that it allows us to empirically test the crypto capabilities of the browser asyncronously at the beginning of runtime, and get only use the features we're sure are implimented. | |
return desiredApis.fulfilledBy(supportedApis); | |
} | |
} | |
module.exports = DetectSubtleCrypto(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment