forked from nayuki/Native-hashes-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha1sum.java
More file actions
70 lines (60 loc) · 1.54 KB
/
sha1sum.java
File metadata and controls
70 lines (60 loc) · 1.54 KB
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
/*
* Native hash functions for Java
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/native-hash-functions-for-java
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import nayuki.nativehash.Sha1;
public class sha1sum {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("Usage: java sha1sum FILES...");
System.exit(1);
}
int status = 0;
for (String arg : args) {
InputStream in = null;
try {
if (arg.equals("-"))
in = System.in;
else {
File file = new File(arg);
if (file.isDirectory()) {
System.err.println("sha1sum: " + arg + ": Is a directory");
status = 1;
continue;
} else if (!file.exists()) {
System.err.println("sha1sum: " + arg + ": No such file or directory");
status = 1;
continue;
} else {
in = new FileInputStream(file);
}
}
Sha1 hasher = new Sha1();
byte[] buf = new byte[64 * 1024];
while (true) {
int n = in.read(buf);
if (n == -1)
break;
hasher.update(buf, 0, n);
}
byte[] hash = hasher.getHash();
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(Integer.toString((b >>> 4) & 0xF, 16))
.append(Integer.toString((b >>> 0) & 0xF, 16));
}
System.out.println(sb.toString() + " " + arg);
} finally {
if (in != null && in != System.in)
in.close();
}
}
System.exit(status);
}
}