Skip to content

Instantly share code, notes, and snippets.

@jonsage
Last active August 20, 2021 12:35
Show Gist options
  • Save jonsage/c66ed87b1356ce10d845cd8eefd2a795 to your computer and use it in GitHub Desktop.
Save jonsage/c66ed87b1356ce10d845cd8eefd2a795 to your computer and use it in GitHub Desktop.
// BROWSER
// ASCII to HEX
const a2h = s => [].map.call(s, c => c.charCodeAt(0).toString(16)).join('');
// a2h('mystring');
// HEX to ASCII
const h2a = s => s.match(/.{1,2}/g).map(c => String.fromCharCode(parseInt(c, 16))).join('');
// h2a('6d79737472696e67');
// ASCII to BASE64
btoa('mystring');
// BASE64 to ASCII
atob('bXlzdHJpbmc=');
// ASCII to SHA256 (HEX)
Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode('mystring')))).map(b => b.toString(16).padStart(2, '0')).join('');
// bd3ff47540b31e62d4ca6b07794e5a886b0f655fc322730f26ecd65cc7dd5c90
// NODE.JS
// ASCII to HEX
Buffer.from('mystring', 'ascii').toString('hex');
// ASCII to BASE64
Buffer.from('mystring', 'ascii').toString('base64');
// HEX to ASCII
Buffer.from('6d79737472696e67', 'hex').toString('ascii');
// HEX to BASE64
Buffer.from('6d79737472696e67', 'hex').toString('base64');
// ASCII to SHA256 (HEX)
require("crypto").createHash("sha256").update("mystring").digest("hex");
// bd3ff47540b31e62d4ca6b07794e5a886b0f655fc322730f26ecd65cc7dd5c90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment