Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
generateKeyPair, | ||
exportPublicKey, | ||
importPublicKey, | ||
exportPrivateKey, | ||
importPrivateKey, | ||
rsaEncrypt, | ||
rsaDecrypt, | ||
generateSalt, | ||
generatePassphraseKey, | ||
generateKey, | ||
exportKey, | ||
importKey, | ||
encrypt, | ||
decrypt | ||
} from "discourse/plugins/discourse-encrypt/lib/keys"; | ||
|
||
QUnit.module("discourse-encrypt:lib:keys"); | ||
|
||
test("generateKeyPair", async assert => { | ||
const [publicKey, privateKey] = await generateKeyPair(); | ||
assert.ok(publicKey instanceof CryptoKey); | ||
assert.ok(privateKey instanceof CryptoKey); | ||
}); | ||
|
||
test("exportPublicKey & importPublicKey", async assert => { | ||
const publicKey = (await generateKeyPair())[0]; | ||
const exported = await exportPublicKey(publicKey); | ||
assert.ok((await importPublicKey(exported)) instanceof CryptoKey); | ||
}); | ||
|
||
test("exportPrivateKey & importPrivateKey", async assert => { | ||
const key = await generatePassphraseKey("passphrase", generateSalt()); | ||
const privateKey = (await generateKeyPair())[1]; | ||
const exported = await exportPrivateKey(privateKey, key); | ||
assert.ok((await importPrivateKey(exported, key)) instanceof CryptoKey); | ||
}); | ||
|
||
test("rsaEncrypt & rsaDecrypt", async assert => { | ||
const [publicKey, privateKey] = await generateKeyPair(); | ||
const plaintext = "this is a message"; | ||
const ciphertext = await rsaEncrypt(publicKey, plaintext); | ||
const plaintext2 = await rsaDecrypt(privateKey, ciphertext); | ||
assert.equal(plaintext, plaintext2); | ||
}); | ||
|
||
test("generateSalt", async assert => { | ||
assert.equal(24, generateSalt().length); | ||
}); | ||
|
||
test("generatePassphraseKey", async assert => { | ||
const key = await generatePassphraseKey("passphrase", generateSalt()); | ||
assert.ok(key instanceof CryptoKey); | ||
}); | ||
|
||
test("generateKey", async assert => { | ||
const key = await generateKey(); | ||
assert.ok(key instanceof CryptoKey); | ||
}); | ||
|
||
test("exportKey & importKey", async assert => { | ||
const [publicKey, privateKey] = await generateKeyPair(); | ||
const key = await generateKey(); | ||
const exported = await exportKey(key, publicKey); | ||
assert.ok((await importKey(exported, privateKey)) instanceof CryptoKey); | ||
}); | ||
|
||
test("encrypt & decrypt", async assert => { | ||
const key = await generateKey(); | ||
const plaintext = "this is a message"; | ||
const ciphertext = await encrypt(key, plaintext); | ||
const plaintext2 = await decrypt(key, ciphertext); | ||
assert.equal(plaintext, plaintext2); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { generateKeyPair } from "discourse/plugins/discourse-encrypt/lib/keys"; | ||
import { | ||
saveKeyPairToIndexedDb, | ||
loadKeyPairFromIndexedDb, | ||
deleteIndexedDb | ||
} from "discourse/plugins/discourse-encrypt/lib/keys_db"; | ||
|
||
QUnit.module("discourse-encrypt:lib:keys_db"); | ||
|
||
test("Indexed Database API", async assert => { | ||
try{ | ||
await deleteIndexedDb(); | ||
} catch (e) {} | ||
|
||
let publicKey, privateKey; | ||
|
||
[publicKey, privateKey] = await loadKeyPairFromIndexedDb(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
assert.equal(null, publicKey); | ||
assert.equal(null, privateKey); | ||
|
||
[publicKey, privateKey] = await generateKeyPair(); | ||
await saveKeyPairToIndexedDb(publicKey, privateKey); | ||
|
||
[publicKey, privateKey] = await loadKeyPairFromIndexedDb(); | ||
assert.ok(publicKey instanceof CryptoKey); | ||
assert.ok(privateKey instanceof CryptoKey); | ||
|
||
try{ | ||
await deleteIndexedDb(); | ||
} catch (e) {} | ||
}); |
destructuring from an await is so cool. makes the code very easy to follow.