-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
281 lines (245 loc) · 8.52 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Generate ready-to-use tags with subresource integrity (SRI) attributes on them.">
<meta property="og:title" content="SRI Hash Generator">
<meta property="og:description" content="Generate ready-to-use tags with subresource integrity (SRI) attributes on them.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://srihash.ardis.lu">
<link rel="icon" href="data:,">
<title>SRI Hash Generator</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
background: hsl(216deg 50% 92%);
inline-size: min(100% - 32px, 960px);
margin-inline: auto;
margin-block: 16px;
}
h1 {
font-size: clamp(2.5rem, 1rem + 4.8vw, 4rem);
font-weight: 300;
margin: 0;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
}
li:not(:last-child)::after {
content: '•';
content: '•' / '';
padding: 8px;
}
a {
color: hsl(216deg 50% 40%);
font-weight: 700;
text-decoration: none;
transition: color 400ms;
&:hover,
&:focus {
color: hsl(216deg 50% 50%);
text-decoration: underline hsl(216deg 50% 35%) solid 2px;
transition: color 200ms;
}
}
form {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-block: 16px;
}
label {
flex-basis: 100%;
font-size: 1.5rem;
font-weight: 300;
}
input {
flex: 1 1 min(100%, 600px);
}
input,
select,
button {
padding: 8px 12px;
font-size: 1rem;
border: 1px solid hsl(216deg 50% 50%);
border-radius: 4px;
}
button {
cursor: pointer;
background: hsl(216deg 60% 75%);
transition: background-color 400ms;
&:hover,
&:focus {
background: hsl(216deg 50% 77%);
transition: background-color 200ms;
}
}
.checkbox-container {
display: none;
flex-basis: 100%;
gap: 4px;
align-items: center;
line-height: 0;
& input {
flex-basis: 0;
inline-size: 16px;
block-size: 16px;
margin: 0;
}
& label {
flex-grow: 1;
font-size: 1.15rem;
font-weight: 400;
}
}
output {
flex-basis: 100%;
padding: 16px;
background: hsl(216deg 80% 98%);
border-radius: 4px;
display: none;
}
output:has(:not(code:empty)) {
display: revert;
}
code {
font-family: monospace;
font-size: 1.15rem;
line-height: 1.5;
white-space: pre-line;
word-break: break-all;
&::highlight(bracket) {
color: hsl(216deg 30% 60%);
}
&::highlight(equal) {
color: hsl(216deg 40% 40%);
}
&::highlight(tag) {
color: hsl(266deg 70% 50%)
}
&::highlight(attribute) {
color: hsl(216deg 70% 50%);
}
&::highlight(value) {
color: hsl(30deg 70% 50%);
}
}
</style>
</head>
<body>
<h1>SRI Hash Generator</h1>
<ul>
<li><a href="https://www.srihash.org">srihash.org</a></li>
<li><a href="https://github.com/ardislu/minimal-srihash">GitHub</a></li>
</ul>
<form>
<label for="url">Enter the URL of the resource you wish to use:</label>
<input id="url" name="url" type="url" required>
<select id="sriHash" name="sriHash" type="select">
<option value="SHA-256">SHA-256</option>
<option value="SHA-384">SHA-384</option>
<option value="SHA-512" selected>SHA-512</option>
</select>
<button>Hash!</button>
<div class="checkbox-container">
<input id="preload" name="preload" type="checkbox">
<label for="preload">Preload?</label>
</div>
<div class="checkbox-container">
<input id="esm" name="esm" type="checkbox">
<label for="esm">JavaScript module?</label>
</div>
<output><code></code></output>
</form>
<script type="module">
const form = document.querySelector('form');
const cache = {};
async function getSri(url) {
if (cache[url] === undefined) {
cache[url] = {};
const blob = await fetch(url).then(r => r.blob());
const buffer = await blob.arrayBuffer();
const hashPromises = [];
for (const algo of ['SHA-256', 'SHA-384', 'SHA-512']) {
hashPromises.push(crypto.subtle.digest(algo, buffer)
.then(hash => `${algo.replace('-', '').toLowerCase()}-${btoa(String.fromCharCode(...new Uint8Array(hash)))}`)
.then(sri => cache[url][algo] = sri)
);
}
await Promise.all(hashPromises);
cache[url].type = blob.type;
}
return cache[url];
}
function highlight(textNode, regex, token) {
const ranges = [];
for (const m of textNode.textContent.matchAll(regex)) {
const r = new Range();
r.setStart(textNode, m.index);
r.setEnd(textNode, m.index + m[0].length);
ranges.push(r);
}
const h = new Highlight(...ranges);
CSS.highlights.set(token, h);
}
function setCode(type, url, sri, preload, esm) {
const code = document.querySelector('code');
switch (`${type},${preload},${esm}`) {
case 'application/javascript,false,false':
case 'text/javascript,false,false': code.textContent = `<script src="${url}" integrity="${sri}" crossorigin><\/script>`; break;
case 'application/javascript,false,true':
case 'text/javascript,false,true': code.textContent = `<script type="module" src="${url}" integrity="${sri}" crossorigin><\/script>`; break;
case 'application/javascript,true,false':
case 'text/javascript,true,false': code.textContent = `<link rel="preload" as="script" href="${url}" integrity="${sri}" crossorigin>`; break;
case 'application/javascript,true,true':
case 'text/javascript,true,true': code.textContent = `<link rel="modulepreload" href="${url}" integrity="${sri}" crossorigin>`; break;
case 'text/css,false,false': code.textContent = `<link rel="stylesheet" href="${url}" integrity="${sri}" crossorigin>`; break;
case 'text/css,true,false': code.textContent = `<link rel="preload" as="style" href="${url}" integrity="${sri}" crossorigin>`; break;
case 'font/otf,true,false':
case 'font/ttf,true,false':
case 'font/woff,true,false':
case 'font/woff2,true,false': code.textContent = `<link rel="preload" as="font" type="${type}" href="${url}" integrity="${sri}" crossorigin>`; break;
default: code.textContent = `<link rel="preload" as="fetch" href="${url}" integrity="${sri}" crossorigin>`; break;
}
if (CSS.highlights !== undefined) {
const textNode = code.childNodes[0];
highlight(textNode, /[<>]\/?/g, 'bracket');
highlight(textNode, /=/g, 'equal');
highlight(textNode, /(link|script)/g, 'tag');
highlight(textNode, /(href|src|integrity|crossorigin|type|rel|as)/g, 'attribute');
highlight(textNode, /".*?"/g, 'value');
}
}
form.addEventListener('submit', async e => {
e.preventDefault();
const { url, sriHash, preload, esm } = e.target.elements;
const { type, ...sri } = await getSri(url.value);
// Reset checkboxes if form submitted via "Hash!" button
if (e?.submitter?.tagName === 'BUTTON') {
preload.checked = false;
esm.checked = false;
if (type === 'text/javascript' || type === 'application/javascript') {
preload.parentElement.style.setProperty('display', 'flex');
esm.parentElement.style.setProperty('display', 'flex');
}
else if (type === 'text/css') {
preload.parentElement.style.setProperty('display', 'flex');
esm.parentElement.style.setProperty('display', 'none');
}
else {
preload.checked = true;
preload.parentElement.style.setProperty('display', 'none');
esm.parentElement.style.setProperty('display', 'none');
}
}
setCode(type, url.value, sri[sriHash.value], preload.checked, esm.checked);
});
// Update code output on option checkbox or hash selection change
document.querySelectorAll('input[type="checkbox"],select').forEach(e => e.addEventListener('change', () => form.requestSubmit()));
</script>
</body>
</html>