forked from mitro-co/keyczarjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser_test.html
108 lines (92 loc) · 4.33 KB
/
browser_test.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!--
Copyright 2014 Lectorius, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="node_modules/node-forge/js/aes.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/sha1.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/sha256.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/md.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/util.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/prng.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/random.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/jsbn.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/pbkdf2.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/hmac.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/asn1.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/oids.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/pkcs1.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/rsa.js"></script>
<script type="text/javascript" src="node_modules/node-forge/js/pki.js"></script>
<script type="text/javascript" src="keyczar_util.js"></script>
<script type="text/javascript" src="keyczar.js"></script>
<script type="text/javascript">
'use strict';
function loaded() {
// generate a small key to make this fast
var options = {
size: 1024
};
document.getElementById('keysize').textContent = options.size;
var start = new Date();
var privateKey = keyczar.create(keyczar.TYPE_RSA_PRIVATE, null, options);
var end = new Date();
document.getElementById('time').textContent = (end - start).toString();
document.getElementById('privatekey').textContent = privateKey.toJson();
document.getElementById('publickey').textContent = privateKey.exportPublicKey().toJson();
var publicKey = keyczar.fromJson(privateKey.exportPublicKey().toJson());
// Test round tripping using the exported key
var message = document.getElementById('input').textContent;
var encrypted = publicKey.encrypt(message);
document.getElementById('encrypted').textContent = encrypted;
privateKey = keyczar.fromJson(privateKey.toJson());
var decrypted = privateKey.decrypt(encrypted);
document.getElementById('decrypted').textContent = decrypted;
var success = true;
if (decrypted != message) {
document.getElementById('error').innerHTML += '<p>Decryption failed?</p>';
success = false;
}
// Encrypt the private key
var password = 'password';
var encryptedKey = privateKey.toJsonEncrypted(password)
document.getElementById('encryptedkey').textContent = encryptedKey;
// Decrypt the key and decrypt the value
var decryptedKey = keyczar.fromJson(encryptedKey, password);
decrypted = decryptedKey.decrypt(encrypted);
if (decrypted != message) {
document.getElementById('error').innerHTML += '<p>Decrypted key failed?</p>';
success = false;
}
if (success) {
document.getElementById('success').textContent = 'SUCCESS!';
}
}
</script>
</head>
<body onload="loaded()">
<span id="success" style="color: green; font-weight: bold;"></span>
<span id="error" style="color: red; font-weight: bold;"></span>
<!-- <p>Input message: <span id="input">hello world?</span></p> -->
<p>Input message: <span id="input">Héllo world? 本 Key Emoji: 🔑</span></p>
<p>Key generation time (<span id="keysize"></span>-bit RSA): <span id="time"></span> ms</p>
<p>Encrypted: <span id="encrypted"></span></p>
<p>Decrypted: <span id="decrypted"></span></p>
<hr>
<p>Public key: <span id="publickey"></span></p>
<p>Private key: <span id="privatekey"></span></p>
<hr>
<p>Encrypted key: <span id="encryptedkey"></span></p>
</body>
</html>