12
12
* Class to demonstrate the use of message digests for passwords.
13
13
*/
14
14
public class PasswordMD {
15
-
16
- /** The digest of a password. */
15
+
16
+ /**
17
+ * The digest of a password.
18
+ */
17
19
protected byte [] digest ;
18
-
19
- /** The algorithm that will create the digest. */
20
+
21
+ /**
22
+ * The algorithm that will create the digest.
23
+ */
20
24
protected MessageDigest md ;
21
-
25
+
22
26
/**
23
27
* Instantiates a new password ,essage digest.
24
28
*
25
- * @param password the password
29
+ * @param password the password
26
30
* @param algorithm the algorithm
27
- * @param provider the provider
31
+ * @param provider the provider
32
+ *
28
33
* @throws GeneralSecurityException the general security exception
29
34
*/
30
35
protected PasswordMD (String password , String algorithm , String provider )
31
- throws GeneralSecurityException {
32
- if (provider == null )
36
+ throws GeneralSecurityException {
37
+ if (provider == null ) {
33
38
md = MessageDigest .getInstance (algorithm );
34
- else
39
+ } else {
35
40
md = MessageDigest .getInstance (algorithm , provider );
41
+ }
36
42
digest = md .digest (password .getBytes ());
37
43
}
38
-
39
- /**
40
- * Gets the size of the digest in bytes.
41
- *
42
- * @return the digest size in bytes
43
- */
44
- public int getDigestSize () {
45
- return digest .length ;
46
- }
47
-
48
- /**
49
- * Gets the digest as a hexadecimal string.
50
- *
51
- * @return the digest as a hexadecimal string
52
- */
53
- public String getDigestAsHexString () {
54
- return new BigInteger (1 , digest ).toString (16 );
55
- }
56
-
57
- /**
58
- * Check a password.
59
- *
60
- * @param password the password
61
- * @return true, if successful
62
- */
63
- public boolean checkPassword (String password ) {
64
- return MessageDigest .isEqual (digest , md .digest (password .getBytes ()));
65
- }
66
-
44
+
67
45
/**
68
46
* Show test.
69
47
*
70
48
* @param algorithm the algorithm
71
- * @throws GeneralSecurityException the general security exception
72
49
*/
73
50
public static void showTestDefault (String algorithm ) {
74
51
try {
@@ -79,20 +56,18 @@ public static void showTestDefault(String algorithm) {
79
56
System .out .println ("Is the password 'password'? "
80
57
+ app .checkPassword ("password" ));
81
58
System .out .println ("Is the password 'secret'? "
82
- + app .checkPassword ("secret" ));
59
+ + app .checkPassword ("secret" ));
83
60
} catch (NoSuchAlgorithmException e ) {
84
61
System .out .println (e .getMessage ());
85
62
} catch (GeneralSecurityException e ) {
86
63
System .out .println (e .getMessage ());
87
64
}
88
65
}
89
66
90
-
91
67
/**
92
68
* Show test.
93
69
*
94
70
* @param algorithm the algorithm
95
- * @throws GeneralSecurityException the general security exception
96
71
*/
97
72
public static void showTestBC (String algorithm ) {
98
73
try {
@@ -103,39 +78,68 @@ public static void showTestBC(String algorithm) {
103
78
System .out .println ("Is the password 'password'? "
104
79
+ app .checkPassword ("password" ));
105
80
System .out .println ("Is the password 'secret'? "
106
- + app .checkPassword ("secret" ));
81
+ + app .checkPassword ("secret" ));
107
82
} catch (NoSuchAlgorithmException e ) {
108
83
System .out .println (e .getMessage ());
109
84
} catch (GeneralSecurityException e ) {
110
85
System .out .println (e .getMessage ());
111
86
}
112
87
}
113
-
88
+
114
89
/**
115
90
* The main method.
116
91
*
117
92
* @param args the arguments
118
93
*/
119
94
public static void main (String [] args ) {
120
- showTestDefault ("MD2" );
121
- showTestDefault ("MD5" );
122
- showTestDefault ("SHA-1" );
123
- showTestDefault ("SHA-224" );
124
- showTestDefault ("SHA-256" );
125
- showTestDefault ("SHA-384" );
126
- showTestDefault ("SHA-512" );
127
- showTestDefault ("RIPEMD128" );
128
- showTestDefault ("RIPEMD160" );
129
- showTestDefault ("RIPEMD256" );
130
- Security .addProvider (new BouncyCastleProvider ());
131
- showTestBC ("MD5" );
132
- showTestBC ("SHA-1" );
133
- showTestBC ("SHA-224" );
134
- showTestBC ("SHA-256" );
135
- showTestBC ("SHA-384" );
136
- showTestBC ("SHA-512" );
137
- showTestBC ("RIPEMD128" );
138
- showTestBC ("RIPEMD160" );
139
- showTestBC ("RIPEMD256" );
95
+ showTestDefault ("MD2" );
96
+ showTestDefault ("MD5" );
97
+ showTestDefault ("SHA-1" );
98
+ showTestDefault ("SHA-224" );
99
+ showTestDefault ("SHA-256" );
100
+ showTestDefault ("SHA-384" );
101
+ showTestDefault ("SHA-512" );
102
+ showTestDefault ("RIPEMD128" );
103
+ showTestDefault ("RIPEMD160" );
104
+ showTestDefault ("RIPEMD256" );
105
+ Security .addProvider (new BouncyCastleProvider ());
106
+ showTestBC ("MD5" );
107
+ showTestBC ("SHA-1" );
108
+ showTestBC ("SHA-224" );
109
+ showTestBC ("SHA-256" );
110
+ showTestBC ("SHA-384" );
111
+ showTestBC ("SHA-512" );
112
+ showTestBC ("RIPEMD128" );
113
+ showTestBC ("RIPEMD160" );
114
+ showTestBC ("RIPEMD256" );
115
+ }
116
+
117
+ /**
118
+ * Gets the size of the digest in bytes.
119
+ *
120
+ * @return the digest size in bytes
121
+ */
122
+ public int getDigestSize () {
123
+ return digest .length ;
124
+ }
125
+
126
+ /**
127
+ * Gets the digest as a hexadecimal string.
128
+ *
129
+ * @return the digest as a hexadecimal string
130
+ */
131
+ public String getDigestAsHexString () {
132
+ return new BigInteger (1 , digest ).toString (16 );
133
+ }
134
+
135
+ /**
136
+ * Check a password.
137
+ *
138
+ * @param password the password
139
+ *
140
+ * @return true, if successful
141
+ */
142
+ public boolean checkPassword (String password ) {
143
+ return MessageDigest .isEqual (digest , md .digest (password .getBytes ()));
140
144
}
141
145
}
0 commit comments