sha256 isomorphic

node v16.18.0
version: master
endpointsharetweet
const SCRIPT_SPECS = [ { // ES Module path: '1.mjs', code: ` const sha256 = async (str) => { const self = {} // Isomorphic polyfill, for node, repl, window, workers if (typeof crypto === 'undefined' || typeof crypto.subtle === 'undefined') { // Node REPL has crypto global const nodeCrypto = await import('crypto') // All Node versions if (nodeCrypto.webcrypto) { self.crypto = nodeCrypto.webcrypto console.log('using Node 15 crypto') } else { console.log('using Node 14 crypto') return nodeCrypto.createHash('sha256').update(str).digest('hex') } } else { self.crypto = crypto // Browser console.log('using browser crypto') } self.TextEncoder = typeof TextEncoder === 'undefined' ? (await import('util')).TextEncoder : TextEncoder // Browser code will work with or without self. const buf = await self.crypto.subtle.digest('SHA-256', new self.TextEncoder('utf-8').encode(str)) return Array.prototype.map.call(new Uint8Array(buf), x => (('00' + x.toString(16)).slice(-2))).join('') } console.log(await sha256('yolo')) ` } ] // END CODE_FILES // <ESM-POLYFILL> Supports TS, MJS, CJS const { execSync } = require("child_process") const os = require("os") // const colour = require("ansicolor") // For debugging rn const OmniLoader = require('runkit-plus-ultra@0.11.4') const sh = cmd => console.log(execSync(cmd).toString()) const omniLoader = new OmniLoader({ scriptSpecs: SCRIPT_SPECS }) omniLoader.saveCode() omniLoader.runCode() // </ESM-POLYFILL>
Loading…

no comments

    sign in to comment