You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

3 years ago
import * as nacl from 'tweetnacl'
const newKey = nacl.randomBytes(nacl.secretbox.keyLength)
console.log(newKey)
function encryptMessage(message: string, key: Uint8Array) {
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength)
const messageUint8 = new TextEncoder().encode(message)
const box = nacl.secretbox(messageUint8, nonce, key)
return { nonce, box }
}
function decryptMessage(nonce: Uint8Array, box: Uint8Array, key: Uint8Array) {
const decryptedMessage = nacl.secretbox.open(box, nonce, key)
if (!decryptedMessage) {
return null
}
return new TextDecoder().decode(decryptedMessage)
}
const encrypted = encryptMessage('hallo zipfl', newKey)
const decrypted = decryptMessage(encrypted.nonce, encrypted.box, newKey)
console.log(decrypted)
const myKeyPair = nacl.box.keyPair()
console.log(myKeyPair)
const createRoom = (roomName: string) => {
const roomKey = nacl.randomBytes(nacl.secretbox.keyLength)
const room = {
name: roomName,
key: roomKey,
users: [
/* TODO self */
],
}
}
3 years ago
async function requestNotificationPermission() {
const permission = await Notification.requestPermission()
if (permission !== 'granted') {
console.error('Notification permission not granted')
return false
}
return true
}
requestNotificationPermission()