-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing-page.ts
166 lines (151 loc) · 5.38 KB
/
hashing-page.ts
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
import { getElement, getRadioButtons, getRadioButtonValue } from './util.js'
import { fromDec, toDec, fromHex, toHex, fromBase64, toBase64, fromAscii, toAscii } from './bytes.js'
import { makeBitmapUpdater } from './bitmap.js'
type InputEncoding = 'dec' | 'hex' | 'base64' | 'ascii'
type OutputEncoding = 'dec' | 'hex' | 'base64'
type HashingAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512'
const input = getElement('textarea#input')
const output = getElement('textarea#output')
const inputEncodingRadioButtons = getRadioButtons('inputencoding')
const outputEncodingRadioButtons = getRadioButtons('outputencoding')
const algorithmRadioButtons = getRadioButtons('algorithm')
const sha1Bitmap: HTMLCanvasElement = getElement('#sha1bitmap')
const sha256Bitmap: HTMLCanvasElement = getElement('#sha256bitmap')
const sha384Bitmap: HTMLCanvasElement = getElement('#sha384bitmap')
const sha512Bitmap: HTMLCanvasElement = getElement('#sha512bitmap')
let currentData = new ArrayBuffer(0)
let currentInputEncoding = getInputEncoding()
input.addEventListener('input', async function () {
let newData
try {
newData = await getData()
} catch (e) {
console.log(e)
alert(`Invalid input for encoding ${currentInputEncoding}`)
writeInput(currentInputEncoding)
return
}
currentData = newData
update()
})
algorithmRadioButtons.forEach(x => x.addEventListener('change', update))
outputEncodingRadioButtons.forEach(x => x.addEventListener('change', update))
inputEncodingRadioButtons.forEach(x => addEventListener('change', async e => {
const newEncoding = getInputEncoding()
if (newEncoding !== currentInputEncoding) {
try {
await writeInput(newEncoding)
} catch (e) {
console.log(e)
currentData = new ArrayBuffer(0)
input.value = ''
update()
}
currentInputEncoding = newEncoding
}
}))
function getInputEncoding(): InputEncoding {
const val = getRadioButtonValue(inputEncodingRadioButtons)
if (val === 'dec' || val === 'hex' || val === 'base64' || val === 'ascii') {
return val
}
throw new Error(`Invalid input encoding ${val}`)
}
function getAlgorithm(): HashingAlgorithm {
const val = getRadioButtonValue(algorithmRadioButtons)
if (val === 'SHA-1' || val === 'SHA-256' || val === 'SHA-384' || val === 'SHA-512') {
return val
}
throw new Error(`Invalid hashing algorithm ${val}`)
}
function getOutputEncoding(): OutputEncoding {
const val = getRadioButtonValue(outputEncodingRadioButtons)
if (val === 'dec' || val === 'hex' || val === 'base64') {
return val
}
throw new Error(`Invalid output encoding ${val}`)
}
async function getData(): Promise<ArrayBuffer> {
switch (currentInputEncoding) {
case 'dec':
return fromDec(input.value)
case 'hex':
return fromHex(input.value)
case 'base64':
return fromBase64(input.value)
case 'ascii':
return fromAscii(input.value)
default:
throw new Error(`Unknown input encoding ${currentInputEncoding}`)
}
}
async function writeInput(encoding: InputEncoding) {
switch (encoding) {
case 'dec':
input.value = toDec(currentData)
break
case 'hex':
input.value = toHex(currentData)
break
case 'ascii':
input.value = toAscii(currentData)
break
case 'base64':
input.value = await toBase64(currentData)
break
default:
throw new Error(`Unknown input encoding ${encoding}`)
}
}
async function update() {
if (currentData.byteLength === 0) {
output.value = ''
}
try {
const hash = await crypto.subtle.digest(getAlgorithm(), currentData)
const outputEncoding = getOutputEncoding()
switch (getOutputEncoding()) {
case 'dec':
output.value = toDec(hash)
break
case 'hex':
output.value = toHex(hash)
break
case 'base64':
output.value = await toBase64(hash)
break
default:
throw new Error(`Unknown output encoding ${outputEncoding}`)
}
for (const canvas of [sha1Bitmap, sha256Bitmap, sha384Bitmap, sha512Bitmap]) {
canvas.style.display = 'none'
}
switch (getAlgorithm()) {
case 'SHA-1':
updateSha1Bitmap(hash)
sha1Bitmap.style.display = 'block'
break
case 'SHA-256':
updateSha256Bitmap(hash)
sha256Bitmap.style.display = 'block'
break
case 'SHA-384':
updateSha384Bitmap(hash)
sha384Bitmap.style.display = 'block'
break
case 'SHA-512':
updateSha512Bitmap(hash)
sha512Bitmap.style.display = 'block'
break
default:
throw new Error(`Unknown hashing algorithm ${getAlgorithm()}`)
}
} catch (e) {
alert(e)
return
}
}
const updateSha1Bitmap = makeBitmapUpdater(sha1Bitmap)
const updateSha256Bitmap = makeBitmapUpdater(sha256Bitmap)
const updateSha384Bitmap = makeBitmapUpdater(sha384Bitmap)
const updateSha512Bitmap = makeBitmapUpdater(sha512Bitmap)