1212 * Class to demonstrate the use of message digests for passwords.
1313 */
1414public class PasswordMD {
15-
16- /** The digest of a password. */
15+
16+ /**
17+ * The digest of a password.
18+ */
1719 protected byte [] digest ;
18-
19- /** The algorithm that will create the digest. */
20+
21+ /**
22+ * The algorithm that will create the digest.
23+ */
2024 protected MessageDigest md ;
21-
25+
2226 /**
2327 * Instantiates a new password ,essage digest.
2428 *
25- * @param password the password
29+ * @param password the password
2630 * @param algorithm the algorithm
27- * @param provider the provider
31+ * @param provider the provider
32+ *
2833 * @throws GeneralSecurityException the general security exception
2934 */
3035 protected PasswordMD (String password , String algorithm , String provider )
31- throws GeneralSecurityException {
32- if (provider == null )
36+ throws GeneralSecurityException {
37+ if (provider == null ) {
3338 md = MessageDigest .getInstance (algorithm );
34- else
39+ } else {
3540 md = MessageDigest .getInstance (algorithm , provider );
41+ }
3642 digest = md .digest (password .getBytes ());
3743 }
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+
6745 /**
6846 * Show test.
6947 *
7048 * @param algorithm the algorithm
71- * @throws GeneralSecurityException the general security exception
7249 */
7350 public static void showTestDefault (String algorithm ) {
7451 try {
@@ -79,20 +56,18 @@ public static void showTestDefault(String algorithm) {
7956 System .out .println ("Is the password 'password'? "
8057 + app .checkPassword ("password" ));
8158 System .out .println ("Is the password 'secret'? "
82- + app .checkPassword ("secret" ));
59+ + app .checkPassword ("secret" ));
8360 } catch (NoSuchAlgorithmException e ) {
8461 System .out .println (e .getMessage ());
8562 } catch (GeneralSecurityException e ) {
8663 System .out .println (e .getMessage ());
8764 }
8865 }
8966
90-
9167 /**
9268 * Show test.
9369 *
9470 * @param algorithm the algorithm
95- * @throws GeneralSecurityException the general security exception
9671 */
9772 public static void showTestBC (String algorithm ) {
9873 try {
@@ -103,39 +78,68 @@ public static void showTestBC(String algorithm) {
10378 System .out .println ("Is the password 'password'? "
10479 + app .checkPassword ("password" ));
10580 System .out .println ("Is the password 'secret'? "
106- + app .checkPassword ("secret" ));
81+ + app .checkPassword ("secret" ));
10782 } catch (NoSuchAlgorithmException e ) {
10883 System .out .println (e .getMessage ());
10984 } catch (GeneralSecurityException e ) {
11085 System .out .println (e .getMessage ());
11186 }
11287 }
113-
88+
11489 /**
11590 * The main method.
11691 *
11792 * @param args the arguments
11893 */
11994 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 ()));
140144 }
141145}
0 commit comments