-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.test.ts
91 lines (75 loc) · 7.54 KB
/
encryption.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { asserts } from './deps.ts'
import { binaryToString, stringToBinary } from './binary.ts'
import {
decryptAsymmetrically,
decryptSymmetrically,
encryptAsymmetrically,
encryptSymmetrically,
generateEncryptionKeyPair,
generateKey,
generateSigningKeyPair,
hashPassword,
sign,
verify,
} from './encryption.ts'
Deno.test({
name: '[encryption] should encrypt and decrypt',
async fn() {
const text = `Hello, I am some text!`
const key = await generateKey()
const encrypted = await encryptSymmetrically(key, stringToBinary(text))
const decrypted = binaryToString(await decryptSymmetrically(key, encrypted))
asserts.assertEquals(decrypted, text)
},
})
Deno.test({
name: '[encryption] should work with password hashing',
async fn() {
const text = `Hello, I am some text!`
const key = await hashPassword('some password here', 'and my salt')
const encrypted = await encryptSymmetrically(key, stringToBinary(text))
const decrypted = binaryToString(await decryptSymmetrically(key, encrypted))
asserts.assertEquals(decrypted, text)
},
})
Deno.test({
name: '[encryption] the same passwords and salts hash should be the same keys',
async fn() {
const password = 'some-password'
const salt = 'some salt'
const hash1 = await hashPassword(password, salt)
const hash2 = await hashPassword(password, salt)
asserts.assertEquals(hash1, hash2)
},
})
Deno.test({
name: '[encryption] emojis should work',
async fn() {
const text =
`Lots of emojis in here: 🌍 📦 🚇 ⬇️ 🌽 🔳 🕡 📅 💱 🐦 🛎 🔛 🌠 🐚 🤑 🏳 🗃 👄 🎆 ♈️ ⛺️ 🍐 🔃 ☂ 📋 🏀 💶 👟 🏍 ❇️ 📻 🌚 🚧 🕣 👘 🚏 ☦ 💞 👃 🏕 🕌 📑 🙏 🚯 📪 🏗 ✖️ 🌯 🕯 🙍 📝 😡 🏟 🔧 🔚 🤘 🐮 🛁 👙 🚌 💚 🐫 🚽 📟 🚬 🏇 📏 🔅 🏖 ☠ 🐣 🍙 😬 💲 🔸 🎦 🈁 🚶 Ⓜ️ 📼 🐛 🚎 🚴 💛 🔖 ✔️ 💒 😅 🔹 🕙 🗾 👋 😣 ⛏ 👎 ✏️ 🔈 🚛 🍼 🐢 ⚗ ⛱ 🍖 🐿 🔰 ⏭ ⛈ ⚔ ☸ 🌝 🍜 🎲 🅰️ 🏙 😲 🆒 🚟 ↙️ 👕 🐯 💖 🍍 💸 ⛹ 🙃 🧀 🏨 💬 ⚖ 🌞 ⛄️ 🔜 ⛓ 💭 🌿 👅 🚩 ✒️ 🐕 🔓 ☝️ ⏬ 🎻 ⚫️ 🔉 🌓 🔶 ⛸ 🏔 1️⃣ 🍒 💃 🚦 🚕 🐠 🏺 👚 💻 🍤 ⏲ 🕤 ♠️ 🎌 👊 🎊 🕉 💰 🎂 ☁️ 📫 🕷 👓 ⛽️ 🍴 🎾 🈹 🚖 ⬜️ 4️⃣ 🎢 🏎 🚆 7️⃣ ♓️ 📢 🐻 🚥 🌁 🍓 ✨ 🖼 🛋 🏡 🌵 😽 🏁 🚝 🚹 🖨 ⛅️ 🔠 🍄 😟 🌪 🎁 ✂️ 😧 ⌨ 📞 🔢 🔥 ⛪️ 🔴 👧 💝 *⃣ 🕊 🖲 📴 👖 🌈 ♏️ 2️⃣ 🐙 ☮ 💅 🍕 🎣 🐥 8️⃣ 🎉 😮 📈 🍳 ♿️ 😱 🌮 🔣 🐋 🌬 📗 🌋 ✅ 🕶 🤐 👮 🐺 🕥 🕓 🙋 🍇 📛 😋 🍲 😾 🐼 6️⃣ 🚾 😑 ⏰ 🗳 ♊️ 🍻 ©️ 🌌 🚰 🔡 🕔 📣 🉐 ✈️ ⚒ 🆑 💨 🙌 😹 📚 ⛩ 🕎 🚠 ⁉️ 🃏 ▶️ 😀 🗑 🕴 🐧 🔐 🌥 ↪️ 🏛 👨 🎍 🌰 ⬆️ 🚃 😈 🙇 😩 🎪 👠 🏐 🐸 ℹ️ 🍶 📳 🎸 ▫️ 💍 🙅 🛤 🐨 🗨 🚺 ⭐️ 🍟 ♍️ 🍈 🌾 🍘 ❎ 💾 🎐 🔗 🙀 🍊 👛 👥 👡 🔫 💣 🔘 🐳 📄 ⛴ 😎 🏥 😊 ☄ 🗓 💴 📃 👱 📒 📥 🕧 😼 💼 🐓 🍺 🐬 🐎 🔟 🗞 🏂 🌡 🚼 🍱 😜 ⏏ 📌 🎤 🛂 😍 🔞 💪 🌼 🤔 🉑 🏊 🐌 ⚙ 🌸 🚲 🤖 🐍 👾 🕋 🏫 🛅 😇 🎏 🏑 🗣 ‼️ ⏺ 🌶 💘 📡 🔩 📓 🐩 🚵 ➕ 📍 👩 ⚪️ ⛳️ 🎃 🛃 🖍 👂 🌃 🏄 🎼 🚳 ⏪ 👫 🌺 🎥 🕕 ⚓️ 🚷 🎳 ◀️ 🕦 🚙 🈴 📷 🌷 🍛 ☢ 💵 ☣ 🌻 ♣️ 🛬 ✳️ 🔌 🏈 🛌 ↕️ 📨 🚈 ⚡️ ✍ 🌜 🎠 🚄 😯 🍣 ➗ 💯 🏣 📬 💤 😿 ↔️ ➰ 🎎 🏩 🏴 🎿 🚚 😵 🌏 💡 🛡 📰 ↖️ 🍢 🌫 👀 ⏳ ❌ 🍮 💗 😝 🏪 🕸 🎗 🏷 ♉️ 🤗 🕵 🎨 🎴 🐰 ✊ 💥 🦂 🎇 🔻 🍝 🐊 👽 👼 🍋 🐷 💂 ™️ 🆙 🚫 🦀 🍹 👈 ⚜ 🐡 🤕 🔤 🔊 ◼️ 🌦 💷 🍌 😞 🤓 🔼 🚒 🎹 🍡 📿 ⚾️ 🏯 🌟 🏏 🏋 🙂 😠 🈷️ 🖖 😖 🆘 〰️ 👦 🔽 ⛲️ 🎋 🌙 🚑 🚗 🔂 🚢 🏰 👿 👝 ⛎ 🐪 😆 ♑️ 💕 🔕 ❕ 🚓 🌛 📕 🀄️ ↗️ 👸 🌔 🖱 ⏯ 😕 👜 ✌️ 🏉 🔪 📆 🍫 ☺️ 👇 🍸 🔮 🕟 🈺 🔍 🗡 ⤵️ 🌐 🛍 ☔️ 🍁 🖐 🌄 📖 👁 🍞 😴 🏘 💦 ✴️ 🐱 💩 👢 🍆 🔺 🗒 ☹ 💑 9️⃣ 🕰 📔 😦 🏠 👏 ⚠️ 🌨 🚊 🌖 😻 👭 😙 🔏 🕖 🎀 🆚 💉 😭 🌱 🕞 😰 🍵 🛫 🚉 🍑 🌹 🍿 👪 🌩 🚻 🔲 🚮 🐏 🎩 🅱️ 🏝 🈯️ 📩 🕠 ♒️ 🍯 💁 🛏 😓 🍂 🚡 🌂 📁 🐶 ❔ 🍅 🙄 ◻️ 🌊 🍗 🈂️ ❣ 😃 🚪 👔 🛐 🌴 📲 💳 👐 ⏱ 😘 🅿️ 🔑 📭 🅾️ ⏫ ⬛️ 💐 🎧 🗼 🗽 🐭 🚁 🌑 🤒 ✉️ 💋 🗿 🏢 ⏩ 📠 💽 🌀 🌲 🏤 🐒 🔔 🕜 📉 🌉 🎛 ↘️ 🚿 🕹 ⚱ 📱 🔄 🚍 🚂 🐔 3️⃣ 🚨 🕗 💈 👶 🖌 🎭 🛀 🎟 🐉 🌎 🙆 👆 🖇 ⌛️ 🔆 💙 🈲 🎙 🚸 🙎 👵 🕳 ⌚️ 💹 🛣 ⏮ 🎓 💓 🚅 🏭 🗻 🍨 🍔 🌭 ♦️ ⤴️ 🐟 🎈 🔀 🚐 🈚️ ®️ ☃ 5️⃣ ☕️ 👻 📂 👌 ⛔️ 🍧 🎷 🗂 🖕 🖊 🌘 😚 🐁 ◾️ 🙊 🎺 🐽 🏜 😶 😔 👹 👉 😸 🗺 🐖 💿 🆕 🎡 🦄 ♐️ 😂 🍥 ㊙️ 🏅 🐐 🎫 🎞 🆗 🐆 📶 😄 🍠 📧 📸 ✋ 🏒 🔇 🐗 💇 🌕 👬 💜 🎰 🎅 🚱 🏆 🐃 🍦 ♋️ ⛑ 🌗 🌅 🆖 💟 👤 🚀 😢 💧 🍉 💢 🐑 ☑️ 🍪 🎬 ↩️ 🗜 🍽 ♻️ 🌆 👞 😌 🔒 🏃 🚜 📯 👗 🛩 🚣 🖥 😒 📊 🈸 💊 👣 🎶 💀 ⬅️ 📎 🔁 🌤 🙈 💏 ㊗️ ⏹ 🕚 💎 📇 🍚 🏚 😳 🎑 🛥 🕘 🖋 📘 ⛰ 🎖 📽 🗝 ➿ 🏌 📀 ▪️ 🕛 😺 🕐 👑 🐲 🐾 ☯ 🚔 ◽️ 😁 🈵 🐹 🐄 🏞 🎮 🗯 🍾 🔦 🛰 🍬 👳 ⚛ ⚰ 🐵 🔯 🕒 🐂 🗄 🍏 😗 〽️ 🐞 📙 🆓 0️⃣ ♥️ ☀️ 💠 🔋 🌧 🎱 🌒 🏮 😪 🏸 🐜 💄 🏧 📵 🏹 ⚽️ 👴 🎚 👺 ✡ ☘ ♌️ ☎️ ♨️ 🕝 📤 🎄 ⛵️ 🌇 💫 📐 🍰 😥 🛢 📺 👷 👲 👍 🍭 ⏸ 🐈 🆔 🍷 🍎 🏓 😷 🔬 🚤 🙁 🍀 👯 🐇 🔭 😨 🎽 🎯 💮 ❄️ 🦁 ❗️ 🏬 🈶 🙉 🔙 ⭕️ ⛷ 🕢 🛄 🍩 🆎 🦃 🚭 🔷 📜 🈳 😛 😏 ✝ 💌 ☪ 🔨 🐅 😉 #️⃣ ♎️ 🛳 🕑 🎵 😐 😤 🌳 🏵 👰 💺 🚋 🔵 🔝 👒 🕍 💆 🛠`
const key = await generateKey()
const encrypted = await encryptSymmetrically(key, stringToBinary(text))
const decrypted = binaryToString(await decryptSymmetrically(key, encrypted))
asserts.assertEquals(decrypted, text)
},
})
Deno.test({
name: '[encryption] public/private key authentication should work',
async fn() {
const text = `Hello there!`
const pair = await generateEncryptionKeyPair()
const encrypted = await encryptAsymmetrically(pair.publicKey, stringToBinary(text))
const decrypted = binaryToString(await decryptAsymmetrically(pair.privateKey, encrypted))
asserts.assertEquals(decrypted, text)
},
})
Deno.test({
name: '[encryption] digital signatures should work',
async fn() {
const pair = await generateSigningKeyPair()
const dataToSign = stringToBinary('something that we want to make sure came from somebody')
const signature = await sign(pair.privateKey, dataToSign)
const isValid = await verify(signature, pair.publicKey, dataToSign)
asserts.assertEquals(isValid, true)
},
})