Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example on how to use the library and fixed the keyboard entropy collector #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added examples/example.css
Empty file.
78 changes: 78 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

function init() {
console.log("INIT");

MochiKit.Signal.connect("doEncrypt", 'onclick', doEncrypt);
MochiKit.Signal.connect("doDecrypt", 'onclick', doDecrypt);

Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();
}


function doEncrypt() {
var key;
var value;

key = MochiKit.DOM.getElement('password').value;
value = MochiKit.DOM.getElement('plaintext').value;

encrypt(key, value, MochiKit.DOM.getElement('encryptedtext'));
}

function doDecrypt() {
var key;
var value;

key = MochiKit.DOM.getElement('password').value;
value = MochiKit.DOM.getElement('encryptedtext').value;

decrypt(key, value, MochiKit.DOM.getElement('plaintext'));
}

function encrypt(aKey, aValue, aTextArea) {
var key, value;
var prng;
var deferredResult;

key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
value = new Clipperz.ByteArray(aValue);
prng = Clipperz.Crypto.PRNG.defaultRandomGenerator();

deferredResult = new Clipperz.Async.Deferred("encrypt", {trace: true});
deferredResult.addCallback(MochiKit.Base.method(prng, 'deferredEntropyCollection'));
deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncrypt, key, value);
deferredResult.addCallback(MochiKit.Async.wait, 0.1);
deferredResult.addCallback(function(aResult) {
aTextArea.value = aResult.toBase64String();
});
deferredResult.addErrback(function(anError) {
aTextArea.value = "ERROR";
})

deferredResult.callback();
}

function decrypt(aKey, aValue, aTextArea) {
var key, value;
var prng;
var deferredResult;

key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
value = new Clipperz.ByteArray().appendBase64String(aValue);
prng = Clipperz.Crypto.PRNG.defaultRandomGenerator();

deferredResult = new Clipperz.Async.Deferred("encrypt", {trace: true});
deferredResult.addCallback(MochiKit.Base.method(prng, 'deferredEntropyCollection'));
deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value);
deferredResult.addCallback(MochiKit.Async.wait, 0.1);
deferredResult.addCallback(function(aResult) {
aTextArea.value = aResult.asString();
});
deferredResult.addErrback(function(anError) {
aTextArea.value = "ERROR";
})

deferredResult.callback();
}

MochiKit.DOM.addLoadEvent(init);
237 changes: 237 additions & 0 deletions examples/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/Clipperz/Async.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*

Copyright 2008-2011 Clipperz Srl
Copyright 2008-2012 Clipperz Srl

This file is part of Clipperz's Javascript Crypto Library.

Expand Down
2 changes: 1 addition & 1 deletion js/Clipperz/Base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*

Copyright 2008-2011 Clipperz Srl
Copyright 2008-2012 Clipperz Srl

This file is part of Clipperz's Javascript Crypto Library.

Expand Down
Loading