forked from daviddahl/web-crypto-ideas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh-level-api.js
More file actions
61 lines (43 loc) · 1.72 KB
/
high-level-api.js
File metadata and controls
61 lines (43 loc) · 1.72 KB
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
// HighLevel API usage example JavaScript
/////////////////////////////////////////
// Get a "keypair" that will be generated if it does not exist:
var myCurrentKeyPair = null;
function onGetKeypair(aKeypair)
{
localStorage.setItem(aKeypair.id, aKeypair.publicKey);
myCurrentKeyPair = aKeypair;
}
var cryptoAPI = window.crypto.highLevel();
cryptoAPI.onGetKeypair = onGetKeypair;
cryptoAPI.getKeypair();
// Create another keypair...
var createdKeyPairs = [];
function onCreateKeypair(aKeypair)
{
localStorage.setItem(aKeypair.id, aKeypair.publicKey);
createdKeyPairs.push(aKeypair);
}
cryptoAPI.onCreateKeypair = onCreateKeypair;
cryptoAPI.createKeypair();
var plainText = "June 2012 had an extra holiday shopping weekend, but registered only a 3.3% improvement over June 2011. Without an extra holiday weekend, July 2012 saw almost identical year-over-year growth; 3.4%.";
function toArrayBuffer(aPlainText) { // returns an Uint8Array
}
var clearData = toArrayBuffer(plainText);
function onEncryptComplete(aCipherData, aPublicKey){
// send cipher data to the server for storage, etc...
}
cryptoAPI.onEncryptComplete = onEncryptComplete;
cryptoAPI.encryptAndSign(clearData, localStorage.getItem('alicePubKey'));
function onDecryptComplete(aPlainText) {
// read and save plain text back to localStorage/IndexedDB
}
function onDecryptError(aException) {
// examine exception raised, re-throw or throw a new error
}
cryptoAPI.onDecryptError = onDecryptError;
// we have recvd a new cipher message...
// set the event handler
cryptoAPI.onDecryptComplete = onDecryptComplete;
// verfiy and decrypt - if verification or decryption fails, onDecryptError is fired
cryptoAPI.decryptAndVerify(cipherMessage);
// TODO: sign, verify, hash, MAC