Skip to content

Instantly share code, notes, and snippets.

@TheZ3ro
Created September 28, 2015 14:01
Show Gist options
  • Save TheZ3ro/2bda23248c7e6acc731a to your computer and use it in GitHub Desktop.
Save TheZ3ro/2bda23248c7e6acc731a to your computer and use it in GitHub Desktop.
Detect the Native Crypto Environment in JavaScript
var CryptoEnv = {};
(function () {
var exports = (typeof module !== 'undefined' && module.exports) || window.CryptoEnv;
var node = (typeof module !== 'undefined');
var crypto;
if(node){
crypto = require('crypto');
}else{
crypto = (window.crypto && crypto.subtle) ||
(window.crypto && crypto.webkitSubtle) ||
(window.msCrypto && window.msCrypto.Subtle);
if (!cryptoSubtle) {
throw new Error('crypto.subtle not found');
}
}
exports.check = function () {
if(node){
console.log("Node.JS");
}else{
console.log("Browser");
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment