This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgenerate-vectors.html
162 lines (135 loc) · 8.29 KB
/
generate-vectors.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function s2b ( s ) {
var b = new Uint8Array(s.length);
for ( var i = 0; i < s.length; i++ ) b[i] = s.charCodeAt(i);
return b;
}
function b2s ( b ) {
if ( b instanceof ArrayBuffer ) b = new Uint8Array(b);
var s = '';
for ( var i = 0; i < b.length; i++ ) s += String.fromCharCode( b[i] );
return s;
}
/**
* RSASSA-PKCS1-v1.5_SHA-256
var rsaSsaAlg2 = { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256', modulusLength: 4096, publicExponent: s2b("\x01\x00\x01") };
crypto.subtle
.generateKey( rsaSsaAlg2, true, [ 'sign', 'verify' ] )
.then( function ( keyPair ) {
crypto.subtle.exportKey( 'spki', keyPair.publicKey )
.then( function ( pubKey ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_spki_pub = '" + btoa( b2s(pubKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'pkcs8', keyPair.privateKey )
.then( function ( prvKey ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_pkcs8_prv = '" + btoa( b2s(prvKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.publicKey )
.then( function ( jwkPubKey ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_jwk_pub = " + JSON.stringify(jwkPubKey) + ";</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.privateKey )
.then( function ( jwkPrvKey ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_jwk_prv = " + JSON.stringify(jwkPrvKey) + ";</code></p>\n" );
});
crypto.subtle.sign( rsaSsaAlg2, keyPair.privateKey, new Uint8Array(0) )
.then( function ( signature ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_signature_empty = '" + btoa( b2s(signature) ) + "';</code></p>\n" );
});
crypto.subtle.sign( rsaSsaAlg2, keyPair.privateKey, s2b("Hello World!") )
.then( function ( signature ) {
document.write( "<p><code>var rsassa_pkcs1_sha256_signature_hello = '" + btoa( b2s(signature) ) + "';</code></p>\n" );
});
});
*/
/**
* AES-CBC
*/
var aesCbcAlg1 = { name: 'AES-CBC', length: 128, iv: crypto.getRandomValues( new Uint8Array(16) ) };
/**
* RSA-OAEP_SHA-1
*/
var rsaOaepAlg1 = { name: 'RSA-OAEP', hash: 'SHA-1', modulusLength: 2048, publicExponent: s2b("\x01\x00\x01") };
crypto.subtle
.generateKey( rsaOaepAlg1, true, [ 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey' ] )
.then( function ( keyPair ) {
crypto.subtle.exportKey( 'spki', keyPair.publicKey )
.then( function ( pubKey ) {
document.write( "<p><code>var rsa_oaep_sha1_spki_pub = '" + btoa( b2s(pubKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'pkcs8', keyPair.privateKey )
.then( function ( prvKey ) {
document.write( "<p><code>var rsa_oaep_sha1_pkcs8_prv = '" + btoa( b2s(prvKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.publicKey )
.then( function ( jwkPubKey ) {
document.write( "<p><code>var rsa_oaep_sha1_jwk_pub = " + JSON.stringify(jwkPubKey) + ";</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.privateKey )
.then( function ( jwkPrvKey ) {
document.write( "<p><code>var rsa_oaep_sha1_jwk_prv = " + JSON.stringify(jwkPrvKey) + ";</code></p>\n" );
});
crypto.subtle.encrypt( rsaOaepAlg1, keyPair.publicKey, new Uint8Array(0) )
.then( function ( ciphertext ) {
document.write( "<p><code>var rsa_oaep_sha1_ciphertext_empty = '" + btoa( b2s(ciphertext) ) + "';</code></p>\n" );
});
crypto.subtle.encrypt( rsaOaepAlg1, keyPair.publicKey, s2b("Hello World!") )
.then( function ( ciphertext ) {
document.write( "<p><code>var rsa_oaep_sha1_ciphertext_hello = '" + btoa( b2s(ciphertext) ) + "';</code></p>\n" );
});
crypto.subtle.generateKey( aesCbcAlg1, true, [ 'encrypt', 'decrypt' ] )
.then( function ( secretKey ) {
// encrypt string with secretKey
crypto.subtle.encrypt( aesCbcAlg1, secretKey, s2b("test") )
.then( function ( ciphertext ) {
document.write( "<p><code>var aes_cbc_128_iv = '" + btoa( b2s(aesCbcAlg1.iv) ) + "';</code></p>\n" );
document.write( "<p><code>var aes_cbc_128_ciphertext_test = '" + btoa( b2s(ciphertext) ) + "';</code></p>\n" );
});
// wrap secretKey with RSA publicKey
crypto.subtle.wrapKey( 'raw', secretKey, keyPair.publicKey, rsaOaepAlg1 )
.then( function ( wrappedKey ) {
document.write( "<p><code>var rsa_oaep_sha1_wrapped_aes_cbc_128_key = '" + btoa( b2s(wrappedKey) ) + "';</code></p>\n" );
});
});
});
/**
* RSA-OAEP_SHA-256
var rsaOaepAlg2 = { name: 'RSA-OAEP', hash: 'SHA-256', modulusLength: 2048, publicExponent: s2b("\x01\x00\x01") };
crypto.subtle
.generateKey( rsaOaepAlg2, true, [ 'encrypt', 'decrypt' ] )
.then( function ( keyPair ) {
crypto.subtle.exportKey( 'spki', keyPair.publicKey )
.then( function ( pubKey ) {
document.write( "<p><code>var rsa_oaep_sha256_spki_pub = '" + btoa( b2s(pubKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'pkcs8', keyPair.privateKey )
.then( function ( prvKey ) {
document.write( "<p><code>var rsa_oaep_sha256_pkcs8_prv = '" + btoa( b2s(prvKey) ) + "';</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.publicKey )
.then( function ( jwkPubKey ) {
document.write( "<p><code>var rsa_oaep_sha256_jwk_pub = " + JSON.stringify(jwkPubKey) + ";</code></p>\n" );
});
crypto.subtle.exportKey( 'jwk', keyPair.privateKey )
.then( function ( jwkPrvKey ) {
document.write( "<p><code>var rsa_oaep_sha256_jwk_prv = " + JSON.stringify(jwkPrvKey) + ";</code></p>\n" );
});
crypto.subtle.encrypt( rsaOaepAlg2, keyPair.publicKey, new Uint8Array(0) )
.then( function ( ciphertext ) {
document.write( "<p><code>var rsa_oaep_sha256_ciphertext_empty = '" + btoa( b2s(ciphertext) ) + "';</code></p>\n" );
});
crypto.subtle.encrypt( rsaOaepAlg2, keyPair.publicKey, s2b("Hello World!") )
.then( function ( ciphertext ) {
document.write( "<p><code>var rsa_oaep_sha256_ciphertext_hello = '" + btoa( b2s(ciphertext) ) + "';</code></p>\n" );
});
});
*/
</script>
</body>
</html>