-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMD5Digest.java
More file actions
59 lines (46 loc) · 1.54 KB
/
Copy pathMD5Digest.java
File metadata and controls
59 lines (46 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
/*
Listing 2. One-Way Encryption The MessageDigest class
can be used to perform one-way encryption. MessageDigests
cannot be directly instantiated but are created by calling
the getInstance() factory method. Encryption is performed by passing
as an argument to the digest() method the raw bytes of a string.
*/
import java.security.*;
/**
* Prints to standard output the MD5 digest of its
* arguments, each on a separate line.
*/
public class MD5Digest {
private MessageDigest __md5;
private StringBuffer __digestBuffer;
public MD5Digest() throws NoSuchAlgorithmException {
__md5 = MessageDigest.getInstance("MD5");
__digestBuffer = new StringBuffer();
}
public String md5crypt(String password) {
int index;
byte[] digest;
__digestBuffer.setLength(0);
digest = __md5.digest(password.getBytes());
for(index = 0; index < digest.length; ++index)
__digestBuffer.append(
Integer.toHexString(digest[index] & 0xff));
return __digestBuffer.toString();
}
public static final void main(String[] args) {
MD5Digest md5;
int argc;
if(args.length < 1) {
System.err.println("Usage: MD5Digest [password] ...");
return;
}
try {
md5 = new MD5Digest();
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
return;
}
for(argc = 0; argc < args.length; ++argc)
System.out.println(md5.md5crypt(args[argc]));
}
}