Skip to content

Commit d2c0d2a

Browse files
committed
Javadoc cleanup of new crypt classes.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1374394 13f79535-47bb-0310-9956-ffa450edef68
1 parent fefb442 commit d2c0d2a

4 files changed

Lines changed: 88 additions & 83 deletions

File tree

src/main/java/org/apache/commons/codec/digest/Crypt.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* GNU libc crypt(3) compatible hash method.
23-
*
23+
* <p>
2424
* See {@link #crypt(String, String)} for further details.
2525
*
2626
* <p>This class is immutable and thread-safe.</p>
@@ -32,29 +32,29 @@ public class Crypt {
3232

3333
/**
3434
* Encrypts a password in a crypt(3) compatible way.
35-
*
35+
* <p>
3636
* A random salt and the default algorithm (currently SHA-512) are used. See
3737
* {@link #crypt(String, String)} for details.
3838
*
3939
* @param keyBytes
40-
* The plaintext password.
41-
* @return The hash value.
40+
* plaintext password
41+
* @return hash value
4242
*/
4343
public static String crypt(byte[] keyBytes) throws Exception {
4444
return crypt(keyBytes, null);
4545
}
4646

4747
/**
4848
* Encrypts a password in a crypt(3) compatible way.
49-
*
49+
* <p>
5050
* A random salt and the default algorithm (currently SHA-512) are used. See
5151
* {@link #crypt(String, String)} for details.
5252
*
5353
* @param keyBytes
54-
* The plaintext password.
54+
* plaintext password
5555
* @param salt
56-
* The salt value
57-
* @return The hash value.
56+
* salt value
57+
* @return hash value
5858
*/
5959
public static String crypt(byte[] keyBytes, String salt) throws Exception {
6060
if (salt == null) {
@@ -72,13 +72,13 @@ public static String crypt(byte[] keyBytes, String salt) throws Exception {
7272

7373
/**
7474
* Calculates the digest using the strongest crypt(3) algorithm.
75-
*
75+
* <p>
7676
* A random salt and the default algorithm (currently SHA-512) are used.
7777
*
7878
* @see #crypt(String, String)
7979
* @param key
80-
* The plaintext password.
81-
* @return The hash value.
80+
* plaintext password
81+
* @return hash value
8282
*/
8383
public static String crypt(String key) throws Exception {
8484
return crypt(key, null);
@@ -96,18 +96,22 @@ public static String crypt(String key) throws Exception {
9696
* <li>DES, the traditional UnixCrypt algorithm is used else with only 2 chars
9797
* <li>Only the first 8 chars of the passwords are used in the DES algorithm!
9898
* </ul>
99-
* The magic strings "$apr1$" and "$2a$" are not recognised by this method as its output should be identical with
100-
* that of the libc implementation.
99+
* The magic strings "$apr1$" and "$2a$" are not recognised by this method as its
100+
* output should be identical with that of the libc implementation.
101101
*
102102
* <p>
103-
* The rest of the salt string is drawn from the set [a-zA-Z0-9./] and is cut at the maximum length of if a "$" sign
104-
* is encountered. It is therefore valid to enter a complete hash value as salt to e.g. verify a password with:
105-
* storedPwd.equals(crypt(enteredPwd, storedPwd))
103+
* The rest of the salt string is drawn from the set [a-zA-Z0-9./] and is cut at the
104+
* maximum length of if a "$" sign is encountered. It is therefore valid to enter a
105+
* complete hash value as salt to e.g. verify a password with:
106+
*
107+
* <pre>
108+
* storedPwd.equals(crypt(enteredPwd, storedPwd))
109+
* </pre>
106110
*
107111
* <p>
108-
* The resulting string starts with the marker string ($6$), continues with the salt value and ends with a "$" sign
109-
* followed by the actual hash value. For DES the string only contains the salt and actual hash. It's toal length is
110-
* dependend on the algorithm used:
112+
* The resulting string starts with the marker string ($6$), continues with the salt
113+
* value and ends with a "$" sign followed by the actual hash value. For DES the string
114+
* only contains the salt and actual hash. It's total length is dependent on the algorithm used:
111115
* <ul>
112116
* <li>SHA-512: 106 chars
113117
* <li>SHA-256: 63 chars
@@ -123,15 +127,16 @@ public static String crypt(String key) throws Exception {
123127
* crypt("secret", "xx") => "xxWAum7tHdIUw"
124128
* </pre>
125129
*
126-
* This method comes in a variation that accepts a byte[] array to support input strings that are not encoded in
127-
* UTF-8 but e.g. in ISO-8859-1 where equal characters result in different byte values.
130+
* <p>
131+
* This method comes in a variation that accepts a byte[] array to support input strings that
132+
* are not encoded in UTF-8 but e.g. in ISO-8859-1 where equal characters result in different byte values.
128133
*
129134
* @see "The man page of the libc crypt (3) function."
130135
* @param key
131-
* The plaintext password as entered by the used.
136+
* plaintext password as entered by the used
132137
* @param salt
133-
* The salt value
134-
* @return The hash value i.e. encrypted password including the salt string
138+
* salt value
139+
* @return hash value, i.e. encrypted password including the salt string
135140
*/
136141
public static String crypt(String key, String salt) throws Exception {
137142
return crypt(key.getBytes(Charsets.UTF_8), salt);

src/main/java/org/apache/commons/codec/digest/Md5Crypt.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@
2929
* Based on the public domain ("beer-ware") C implementation from Poul-Henning Kamp which was found at:
3030
* </p>
3131
* <p>
32-
* http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain</br>
32+
* http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain<br/>
3333
* Source: $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $
3434
* </p>
35-
* <p>
36-
* Conversion to Kotlin and from there to Java in 2012.
37-
* </p>
38-
* <p>
39-
* The C style comments are from the original C code, the ones with "//" from the port.
40-
* </p>
35+
* <p>Conversion to Kotlin and from there to Java in 2012.</p>
36+
*
37+
* <p>The C style comments are from the original C code, the ones with "//" from the port.</p>
4138
*
4239
* <p>This class is immutable and thread-safe.</p>
4340
*
@@ -91,24 +88,25 @@ public static String apr1Crypt(String keyBytes) throws Exception {
9188
}
9289

9390
/**
94-
* Generates an Apache htpasswd compatible "$apr1$" MD5 based hash value. *
95-
*
96-
* The algorithm is identical to the crypt(3) "$1$" one but produces different outputs due to the different salt
97-
* prefix.
91+
* Generates an Apache htpasswd compatible "$apr1$" MD5 based hash value.
92+
* <p>
93+
* The algorithm is identical to the crypt(3) "$1$" one but produces different
94+
* outputs due to the different salt prefix.
9895
*
9996
* @param keyBytes
100-
* The plaintext string that should be hashed.
97+
* plaintext string that should be hashed.
10198
* @param salt
102-
* Salt string including the prefix and optionally garbage at the end. Will be generated randomly if
103-
* null.
99+
* salt string including the prefix and optionally garbage at the end.
100+
* Will be generated randomly if null.
101+
* @return computed hash value
104102
*/
105103
public static String apr1Crypt(String keyBytes, String salt) throws Exception {
106104
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8), salt);
107105
}
108106

109107
/**
110108
* Generates a libc6 crypt() compatible "$1$" hash value.
111-
*
109+
* <p>
112110
* See {@link Crypt#crypt(String, String)} for details.
113111
*/
114112
public static String md5Crypt(final byte[] keyBytes) throws Exception {
@@ -117,22 +115,23 @@ public static String md5Crypt(final byte[] keyBytes) throws Exception {
117115

118116
/**
119117
* Generates a libc crypt() compatible "$1$" MD5 based hash value.
120-
*
118+
* <p>
121119
* See {@link Crypt#crypt(String, String)} for details.
122120
*
123121
* @param keyBytes
124-
* The plaintext string that should be hashed.
122+
* plaintext string that should be hashed.
125123
* @param salt
126-
* Salt string including the prefix and optionally garbage at the end. Will be generated randomly if
127-
* null.
124+
* salt string including the prefix and optionally garbage at the end.
125+
* Will be generated randomly if null.
126+
* @return computed hash value
128127
*/
129128
public static String md5Crypt(byte[] keyBytes, String salt) throws Exception {
130129
return md5Crypt(keyBytes, salt, MD5_PREFIX);
131130
}
132131

133132
/**
134133
* Generates a libc6 crypt() "$1$" or Apache htpasswd "$apr1$" hash value.
135-
*
134+
* <p>
136135
* See {@link Crypt#crypt(String, String)} or {@link #apr1Crypt(String, String)} for details.
137136
*/
138137
public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix) throws Exception {

src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class Sha2Crypt {
8484

8585
/**
8686
* Generates a libc crypt() compatible "$5$" hash value with random salt.
87-
*
87+
* <p>
8888
* See {@link Crypt#crypt(String, String)} for details.
8989
*/
9090
public static String sha256Crypt(byte[] keyBytes) throws Exception {
@@ -93,7 +93,7 @@ public static String sha256Crypt(byte[] keyBytes) throws Exception {
9393

9494
/**
9595
* Generates a libc6 crypt() compatible "$5$" hash value.
96-
*
96+
* <p>
9797
* See {@link Crypt#crypt(String, String)} for details.
9898
*/
9999
public static String sha256Crypt(byte[] keyBytes, String salt) throws Exception {
@@ -105,23 +105,24 @@ public static String sha256Crypt(byte[] keyBytes, String salt) throws Exception
105105

106106
/**
107107
* Generates a libc6 crypt() compatible "$5$" or "$6$" SHA2 based hash value.
108-
*
109-
* This is a nearly line by line conversion of the original C function. The numbered comments are from the algorithm
110-
* description, the short C-style ones from the original C code and the ones with "Remark" from me.
111-
*
108+
* <p>
109+
* This is a nearly line by line conversion of the original C function. The numbered comments
110+
* are from the algorithm description, the short C-style ones from the original C code and the
111+
* ones with "Remark" from me.
112+
* <p>
112113
* See {@link Crypt#crypt(String, String)} for details.
113114
*
114115
* @param keyBytes
115-
* The plaintext that should be hashed.
116+
* plaintext that should be hashed
116117
* @param salt_string
117-
* The real salt value without prefix or "rounds=".
118+
* real salt value without prefix or "rounds="
118119
* @param saltPrefix
119-
* Either $5$ or $6$.
120+
* either $5$ or $6$
120121
* @param blocksize
121-
* A value that differs between $5$ and $6$.
122+
* a value that differs between $5$ and $6$
122123
* @param algorithm
123-
* The MessageDigest algorithm identifier string.
124-
* @return The complete hash value including prefix and salt.
124+
* {@link MessageDigest} algorithm identifier string
125+
* @return complete hash value including prefix and salt
125126
*/
126127
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm)
127128
throws Exception {
@@ -494,7 +495,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
494495

495496
/**
496497
* Generates a libc crypt() compatible "$6$" hash value with random salt.
497-
*
498+
* <p>
498499
* See {@link Crypt#crypt(String, String)} for details.
499500
*/
500501
public static String sha512Crypt(byte[] keyBytes) throws Exception {
@@ -503,7 +504,7 @@ public static String sha512Crypt(byte[] keyBytes) throws Exception {
503504

504505
/**
505506
* Generates a libc6 crypt() compatible "$6$" hash value.
506-
*
507+
* <p>
507508
* See {@link Crypt#crypt(String, String)} for details.
508509
*/
509510
public static String sha512Crypt(byte[] keyBytes, String salt) throws Exception {

src/main/java/org/apache/commons/codec/digest/UnixCrypt.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
/**
2424
* Unix crypt(3) algorithm implementation.
2525
*
26-
* This class only implements the traditional 56 bit DES based algorithm. Please
26+
* <p>This class only implements the traditional 56 bit DES based algorithm. Please
2727
* use DigestUtils.crypt() for a method that distinguishes between all the
28-
* algorithms supported in the current glibc's crypt().
28+
* algorithms supported in the current glibc's crypt().</p>
2929
*
30-
* The Java implementation was taken from the JetSpeed Portal project (see
31-
* org.apache.jetspeed.services.security.ldap.UnixCrypt).
30+
* <p>The Java implementation was taken from the JetSpeed Portal project (see
31+
* org.apache.jetspeed.services.security.ldap.UnixCrypt).</p>
3232
*
33-
* This class is slightly incompatible if the given salt contains characters
34-
* that are not part of the allowed range [a-zA-Z0-9./].
33+
* <p>This class is slightly incompatible if the given salt contains characters
34+
* that are not part of the allowed range [a-zA-Z0-9./].</p>
3535
*
3636
* <p>This class is immutable and thread-safe.</p>
3737
*
@@ -342,28 +342,27 @@ private static void permOp(int a, int b, int n, int m, int results[]) {
342342

343343
/**
344344
* Generates a crypt(3) compatible hash using the DES algorithm.
345-
*
345+
* <p>
346346
* As no salt is given, a random one will be used.
347347
*
348-
* @param original Plaintext password
349-
*
350-
* @return A 13 character string starting with the salt string.
348+
* @param original
349+
* plaintext password
350+
* @return a 13 character string starting with the salt string
351351
*/
352352
public static String crypt(byte[] original) {
353353
return crypt(original, null);
354354
}
355355

356356
/**
357357
* Generates a crypt(3) compatible hash using the DES algorithm.
358-
*
358+
* <p>
359359
* Using unspecified characters as salt results incompatible hash values.
360360
*
361-
* @param original Plaintext password
362-
*
363-
* @param salt A two character string drawn from [a-zA-Z0-9./] or null for a
364-
* random one.
365-
*
366-
* @return A 13 character string starting with the salt string.
361+
* @param original
362+
* plaintext password
363+
* @param salt
364+
* a two character string drawn from [a-zA-Z0-9./] or null for a random one
365+
* @return a 13 character string starting with the salt string
367366
*/
368367
public static String crypt(byte[] original, String salt) {
369368
if (salt == null) {
@@ -424,12 +423,12 @@ public static String crypt(byte[] original, String salt) {
424423

425424
/**
426425
* Generates a crypt(3) compatible hash using the DES algorithm.
427-
*
426+
* <p>
428427
* As no salt is given, a random one is used.
429428
*
430-
* @param original Plaintext password
431-
*
432-
* @return A 13 character string starting with the salt string.
429+
* @param original
430+
* plaintext password
431+
* @return a 13 character string starting with the salt string
433432
*/
434433
public static String crypt(String original) throws Exception {
435434
return crypt(original.getBytes(Charsets.UTF_8));
@@ -438,10 +437,11 @@ public static String crypt(String original) throws Exception {
438437
/**
439438
* Generates a crypt(3) compatible hash using the DES algorithm.
440439
*
441-
* @param original Plaintext password
442-
* @param salt A two character string drawn from [a-zA-Z0-9./] or null for a
443-
* random one.
444-
* @return A 13 character string starting with the salt string.
440+
* @param original
441+
* plaintext password
442+
* @param salt
443+
* a two character string drawn from [a-zA-Z0-9./] or null for a random one
444+
* @return a 13 character string starting with the salt string
445445
*/
446446
public static String crypt(String original, String salt) throws Exception {
447447
return crypt(original.getBytes(Charsets.UTF_8), salt);

0 commit comments

Comments
 (0)