summaryrefslogtreecommitdiffstats
path: root/test/hash-file.html
blob: acc112154ce45dcaf774dad8b619a48997cbbd6e (plain)
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
<!DOCTYPE html>
<html>
	<head>
		<title>jsSHA (http://caligatio.github.com/jsSHA/) - File Test</title>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <script type="text/javascript" src="../src/sha.js"></script>
        <script type="text/javascript" >
            // How many bytes to read per chunk
            var chunkSize = Math.pow(10, 5)

            // Handle various I/O problems
            function errorHandler(evt) {
                switch(evt.target.error.code) {
                    case evt.target.error.NOT_FOUND_ERR:
                        alert('File Not Found!')
                        break
                    case evt.target.error.NOT_READABLE_ERR:
                        alert('File is not readable')
                        break
                    case evt.target.error.ABORT_ERR:
                        break // noop
                    default:
                        alert('An error occurred reading this file.')
                }
            }

            // Recurse through async chunk reads
            function readFile(hasher1, hasher224, hasher256, hasher384, hasher512, file, start, stop) {
                var progress = document.querySelector('#progress')
                // Only read to the end of the file
                stop = (stop <= file.size) ? stop : file.size

                // Prepare to read chunk
                var reader = new FileReader()
                reader.onerror = errorHandler

                // If we use onloadend, we need to check the readyState.
                reader.onloadend = function(evt) {
                    if (evt.target.readyState == FileReader.DONE) {
                        hasher1.update(evt.target.result)
                        hasher224.update(evt.target.result)
                        hasher256.update(evt.target.result)
                        hasher384.update(evt.target.result)
                        hasher512.update(evt.target.result)

                        var percent = Math.round((stop / file.size) * 100)
                        progress.innerHTML = 'Progress: ' + percent + '%'

                        // Recurse or finish
                        if (stop == file.size) {
                            progress.innerHTML = "";
                            result1 = hasher1.getHash('HEX')
                            result224 = hasher224.getHash('HEX')
                            result256 = hasher256.getHash('HEX')
                            result384 = hasher384.getHash('HEX')
                            result512 = hasher512.getHash('HEX')

                            progress.innerHTML += '<p>SHA-1: ' + result1 + '</p>';
                            progress.innerHTML += '<p>SHA-224: ' + result224 + '</p>';
                            progress.innerHTML += '<p>SHA-256: ' + result256 + '</p>';
                            progress.innerHTML += '<p>SHA-384: ' + result384 + '</p>';
                            progress.innerHTML += '<p>SHA-512: ' + result512 + '</p>';
                        } else {
                            readFile(hasher1, hasher224, hasher256, hasher384, hasher512, file, start + chunkSize, stop + chunkSize)
                        }
                    }
                }

                // Begin read
                var blob = file.slice(start, stop)
                reader.readAsArrayBuffer(blob)
            }

            function handleFileSelect(input) {
                var progress = document.querySelector('#progress')
                // Reset progress indicator on new file selection.
                progress.innerHTML = 'Progress: 0%';

                // Get file object from the form
                var file = input.files[0];

                var hasher1 = new jsSHA('SHA-1', 'ARRAYBUFFER')
                var hasher224 = new jsSHA('SHA-224', 'ARRAYBUFFER')
                var hasher256 = new jsSHA('SHA-256', 'ARRAYBUFFER')
                var hasher384 = new jsSHA('SHA-384', 'ARRAYBUFFER')
                var hasher512 = new jsSHA('SHA-512', 'ARRAYBUFFER')

                // Read file in chunks
                readFile(hasher1, hasher224, hasher256, hasher384, hasher512, file, 0, chunkSize)
            }
        </script>
        <style type="text/css">
            .correct
            {
                color: #00FF00;
                background-color: #FFFFFF;
            }

            .incorrect
            {
                color: #FF0000;
                background-color: #FFFFFF;
            }
        </style>
    </head>
    <body>
        Drop this project's LICENSE file to test, or any other file to find its hashes:
        <p>
            <input type="file" id="files" name="file" onchange="handleFileSelect(this)" />
        </p>
        NOTE: This may not work in Internet Explorer
        <div id="progress"></div>
    </body>
</html>