Write a sha512 function in js. Check if it's in nodejs, then webcrypto, otherwise use…
- User
Write a sha512 function in js. Check if it's in nodejs, then webcrypto, otherwise use third-party tools as fallback
Assistant
Here is a sha512 function in JavaScript that checks for nodejs and webcrypto and falls back to a third party library:
function sha512(data) { if (typeof require === 'function' && typeof module === 'object') { // Nodejs - use built in crypto const crypto = require('crypto') return crypto.createHash('sha512').update(data).digest('hex') } else if (window.crypto && window.crypto.subtle) { // Web crypto - use webcrypto API return window.crypto.subtle.digest({name: 'SHA-512'}, data) .then(hash => { let hashArray = Array.from(new Uint8Array(hash)); return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); }) } else { // Fallback to third party library return require('js-sha512')(data) } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.