Skip to content

Commit 0ed5adc

Browse files
committed
fix md5 authentication, remove guava deps
Previously, the code looking for $MD5$ preambles would always return false, as the '$' characters were quoted as if they were being passed to a regexp library. Splitter.on() did not take a regex, so this pattern never matched.
1 parent 8f1fffb commit 0ed5adc

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/main/java/au/com/southsky/jfreesane/SanePasswordEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static String derivePassword(String salt, String password) {
5050
try {
5151
MessageDigest md = MessageDigest.getInstance("MD5");
5252
md.update(StandardCharsets.ISO_8859_1.encode(salt));
53-
md.update(StandardCharsets.ISO_8859_1.encode(CharBuffer.wrap(password)));
53+
md.update(password.getBytes(StandardCharsets.ISO_8859_1));
5454
return encodeAsHex(md.digest());
5555
} catch (NoSuchAlgorithmException ex) {
5656
// This is not expected, so convert to RuntimeException

src/main/java/au/com/southsky/jfreesane/SaneSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package au.com.southsky.jfreesane;
22

3-
import com.google.common.base.Splitter;
43
import java.awt.image.BufferedImage;
54
import java.io.BufferedInputStream;
65
import java.io.Closeable;
@@ -22,6 +21,7 @@ public final class SaneSession implements Closeable {
2221

2322
private static final int READ_BUFFER_SIZE = 1 << 20; // 1mb
2423
private static final int DEFAULT_PORT = 6566;
24+
private static final String MD5_PREFIX = "$MD5$";
2525

2626
private final Socket socket;
2727
private final SaneOutputStream outputStream;
@@ -374,13 +374,13 @@ boolean authorize(String resource) throws IOException {
374374
* @throws IOException
375375
*/
376376
private void writePassword(String resource, String password) throws IOException {
377-
List<String> resourceParts = Splitter.on("\\$MD5\\$").splitToList(resource);
378-
if (resourceParts.size() == 1) {
377+
int markerIdx = resource.indexOf(MD5_PREFIX);
378+
if (markerIdx > -1) {
379+
outputStream.write(
380+
MD5_PREFIX + SanePasswordEncoder.derivePassword(resource.substring(markerIdx + MD5_PREFIX.length()), password));
381+
} else {
379382
// Write in clean
380383
outputStream.write(password);
381-
} else {
382-
outputStream.write(
383-
"$MD5$" + SanePasswordEncoder.derivePassword(resourceParts.get(1), password));
384384
}
385385
}
386386

0 commit comments

Comments
 (0)