-
Notifications
You must be signed in to change notification settings - Fork 17
/
rsasignaturepsssign.html
153 lines (143 loc) · 7.09 KB
/
rsasignaturepsssign.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>RSA 2048 signature PSS SHA-256 (signature only)</title>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
h2 {font-size: 200%;}
p {font-size: 150%; }
button {height: 100%; font-size: 150%;}
textarea {font-size: 150%;}
input[type="text"] {font-size: 100%; }
input[type="button"] {font-size: 100%; }
</style>
</head>
<body>
<h1>RSA 2048 signature PSS SHA-256 (signature only)</h1>
<hr><p>
<b>Important note: this program is doing what it promises but the programming itself is of very poor quality and for demonstration purposes only. Never ever use this program as source for your own programs because there are a lot of conversions to get it run.</b>
<br><br>Get more information about this program on <a href="https://github.com/java-crypto/cross_platform_crypto/blob/main/docs/rsa_signature_pss_string.md" target="_blank">
my webpage <b>RSA string signature with PSS padding</b></a><br>
</p><hr>
<p>Insert your own Private Key (in PEM encoding):</p>
<textarea name="privatekey-value" id="privatekey-value" rows="10" cols="40">-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG2FAY3YJZIxnBH+9j1XS6f+ewjGFlJY4f2IrOpS1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hvihgA6ZRNid58EKuZJBbe/3ek4/nR3A0oAVwZZMNGIH972P7cSZmb/uJXMOQKBgQCsFaQAL+4sN/TUxrkAkylqF+QJmEZ26l2nrzHZjMWROYNJcsn8/XkaEhD4vGSnazCu/B0vU6nMppmezF9Mhc112YSrw8QFK5GOc3NGNBoueqMYy1MG8Xcbm1aSMKVv8xbarh+BZQbxy6x61CpCfaT9hAoA6HaNdeoU6y05lBz1DQKBgAbYiIk56QZHeoZKiZxy4eicQS0sVKKRb24ZUd+04cNSTfeIuuXZrYJ48Jbr0fzjIM3EfHvLgh9rAZ+aHe/L84Ig17KiExe+qyYHjut/SC0wODDtzM/jtrpqyYa5JoEpPIaUSgPuTH/WhO3cDsx63PIW4/CddNs8mCSBOqTnoaxh
-----END PRIVATE KEY-----</textarea>
<section class="import-key spki">
<p>Enter a message to sign:</p>
<input type="text" id="plaintext" name="plaintext" size="50" value="The quick brown fox jumps over the lazy dog">
<hr>
<p><b>Instructions:</b><br>
1.: insert your own private and press the "Import key" button<br>
2.: if the private key is valid the "Sign" button is enabled<br>
3.: enter the message to sign<br>
4.: press the "Sign" button<br>
5.: receive the signature in Base64 encoding</p>
<hr>
<input class="import-key-button" type="button" value="Import Key">
<input class="sign-button hidden" type="button" value="Sign" disabled>
<hr>
</section>
<p>signature (in Base64 encoding):</p>
<textarea name="signatureBase64" id="signatureBase64" rows="10" cols="40">the signature in Base64 encoding comes here...</textarea>
<hr><p><b>Technical note: this program uses the RSA signature algorithm with a 2048 bit long key and PSS signature padding.</b></p><hr>
</body>
<SCRIPT LANGUAGE="JavaScript">
const bufToB64 = buf =>
btoa(Array.prototype.map.call(buf, ch => String.fromCharCode(ch)).join(""));
/*
The unwrapped signing key.
*/
let privatekey;
const signButton = document.querySelector(".spki .sign-button");
/*
Convert a string into an ArrayBuffer
from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
*/
function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
/*
Import a PEM encoded RSA private key, to use for RSA-PSS signing.
Takes a string containing the PEM encoded key, and returns a Promise
that will resolve to a CryptoKey representing the private key.
*/
function importPrivateKey(pem) {
// fetch the part of the PEM string between header and footer
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);
// base64 decode the string to get the binary data
const binaryDerString = window.atob(pemContents);
// convert from a binary string to an ArrayBuffer
const binaryDer = str2ab(binaryDerString);
return window.crypto.subtle.importKey(
"pkcs8",
binaryDer,
{
name: "RSA-PSS",
hash: "SHA-256"
},
true,
["sign"]
);
}
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for the encrypt operation.
*/
function getMessageEncoding() {
const messageBox = document.querySelector("#plaintext");
const message = messageBox.value;
const enc = new TextEncoder();
return enc.encode(message);
}
/*
Get the encoded message-to-sign, sign it and display a representation
of the first part of it in the "signature" element.
*/
async function signMessage() {
let encoded = getMessageEncoding();
const signature = await window.crypto.subtle.sign(
{
name: "RSA-PSS",
saltLength: 32,
},
privateKey,
encoded
);
document.getElementById("signatureBase64").value = bufToB64(new Uint8Array(signature));
}
/*
Show and enable the sign button.
*/
function enableSignButton() {
signButton.classList.add('fade-in');
signButton.addEventListener('animationend', () => {
signButton.classList.remove('fade-in');
});
signButton.removeAttribute("disabled");
signButton.classList.remove("hidden");
}
/*
When the user clicks "Import Key"
- import the key
- enable the "Sign" button
*/
const importKeyButton = document.querySelector(".spki .import-key-button");
importKeyButton.addEventListener("click", async () => {
var pemEncodedKeyTextarea = document.getElementById("privatekey-value").value;
privateKey = await importPrivateKey(pemEncodedKeyTextarea);
enableSignButton();
});
signButton.addEventListener("click", signMessage);
</SCRIPT>
</html>