This file contains hidden or 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
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({ |
This file contains hidden or 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
// 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) { | |
This file contains hidden or 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
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); | |
} |
This file contains hidden or 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
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=>{ |
This file contains hidden or 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
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); | |
})); |
This file contains hidden or 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
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(''); |