1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-27 03:12:23 -05:00
mail/test/unit/rsa-test.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2013-06-10 16:02:29 -04:00
define(['cryptoLib/rsa'], function(rsa) {
'use strict';
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
module("RSA Crypto");
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
var rsaTest = {
keySize: 1024,
testMessage: '06a9214036b8a15b512e03d534120006'
};
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
asyncTest("Generate keypair", 1, function() {
rsa.generateKeypair(rsaTest.keySize, function(err) {
ok(!err);
start();
});
2013-05-14 10:05:31 -04:00
});
2013-06-10 16:02:29 -04:00
test("Export keys", 2, function() {
rsaTest.keypair = rsa.exportKeys();
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
ok(rsaTest.keypair.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0, rsaTest.keypair.pubkeyPem);
ok(rsaTest.keypair.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0, rsaTest.keypair.privkeyPem);
});
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
test("Init", 2, function() {
rsa.init(rsaTest.keypair.pubkeyPem, rsaTest.keypair.privkeyPem);
var exported = rsa.exportKeys();
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
ok(exported.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0);
ok(exported.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0);
});
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
test("Encrypt", 1, function() {
rsaTest.ct = rsa.encrypt(rsaTest.testMessage);
ok(rsaTest.ct);
});
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
test("Decrypt", 1, function() {
var pt = rsa.decrypt(rsaTest.ct);
equal(pt, rsaTest.testMessage);
});
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
test("Sign", 1, function() {
rsaTest.sig = rsa.sign([btoa('iv'), btoa(rsaTest.testMessage)]);
ok(rsaTest.sig);
});
2013-05-14 10:05:31 -04:00
2013-06-10 16:02:29 -04:00
test("Verify", 1, function() {
var res = rsa.verify([btoa('iv'), btoa(rsaTest.testMessage)], rsaTest.sig);
ok(res);
});
2013-05-14 10:05:31 -04:00
});