Skip to content

Instantly share code, notes, and snippets.

@themikefuller
themikefuller / sha.js
Created October 15, 2018 01:23
SHA-1 and SHA-256 Hash Function in JavaScript using crypto.subtle.digest
async function SHA1(text) {
let data = new TextEncoder().encode(text.toString());
let digest = await crypto.subtle.digest("SHA-1",data);
return Array.from(new Uint8Array(digest)).map(val=>{return ('00' + val.toString(16)).slice(-2)}).join('');
}
async function SHA256(text) {
let data = new TextEncoder().encode(text.toString());
let digest = await crypto.subtle.digest("SHA-256",data);
return Array.from(new Uint8Array(digest)).map(val=>{return ('00' + val.toString(16)).slice(-2)}).join('');
@themikefuller
themikefuller / base64url.js
Created October 15, 2018 01:38
Encode and decode byte arrays to and from base64url encoding in JavaScript.
function base64EncodeURL(byteArray) {
return btoa(Array.from(new Uint8Array(byteArray)).map(val => {
return String.fromCharCode(val);
}).join('')).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
}
function base64DecodeURL(b64urlstring) {
return new Uint8Array(atob(b64urlstring.replace(/-/g, '+').replace(/_/g, '/')).split('').map(val => {
return val.charCodeAt(0);
}));
@themikefuller
themikefuller / hexencoding.js
Created October 15, 2018 01:48
Encoding and decoding strings into hex encoding.
function string2hex(text) {
let encoded = new TextEncoder().encode(text);
let hex = Array.from(encoded).map(val=>{
return ('00' + val.toString(16)).slice(-2);
}).join('');
return hex;
}
function hex2string(hex) {
let byteArray = new Uint8Array(hex.match(/.{0,2}/g).map(val=>{
@themikefuller
themikefuller / envelope.js
Last active June 3, 2022 03:07
Sealed Envelope function demonstrating end-to-end encryption with deniability using Starbase Cryptic
// Envelope Function
// End-to-End Encrypted Messaging with Deniability
// Demo showing off the features of Starbase Cryptic
// NOTE: This is just an example. A more secure messaging system would include a forward secrecy layer.
// This is a simplified version of the Sealed Envelope feature implemented within Starbase Encryption.
// Starbase Encryption is based on (inspired by) the Signal Protocol's Double Ratchet and Triple Diffie-Hellman key exchange.
function Envelope(cryptic = null) {
@themikefuller
themikefuller / example.js
Created September 28, 2023 17:09
Code pulled from https://github.com/StarbaseAlpha/Cryptic to give encryption example (without library)
async function Example() {
const encode = (byteArray) => {
return btoa(Array.from(new Uint8Array(byteArray)).map(val => {
return String.fromCharCode(val);
}).join('')).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
};
const createECDH = async (curve = "P-256") => {
let DH = await crypto.subtle.generateKey({
@themikefuller
themikefuller / aes-gcm.js
Created October 15, 2018 02:07
AES-GCM Encryption and Decryption Examples using Web Crypto (subtle.crypto) JavaScript API
async function generateKey() {
return await crypto.subtle.generateKey({
"name":"AES-GCM",
"length":256
},true,['encrypt','decrypt']);
}
async function exportKey(key) {
return await crypto.subtle.exportKey('jwk', key);
}