Created
September 28, 2015 14:01
-
-
Save TheZ3ro/2bda23248c7e6acc731a to your computer and use it in GitHub Desktop.
Detect the Native Crypto Environment in JavaScript
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
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