-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtpassword.java
More file actions
62 lines (49 loc) · 1.83 KB
/
Htpassword.java
File metadata and controls
62 lines (49 loc) · 1.83 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
60
61
62
package server.configuration;
import java.util.HashMap;
import java.util.Base64;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.io.IOException;
public class Htpassword extends ConfigurationReader {
private HashMap<String, String> passwords;
public Htpassword( String filename ) throws IOException {
super( filename );
System.out.println( "Password file: " + filename );
this.passwords = new HashMap<String, String>();
this.load();
}
protected void parseLine( String line ) {
String[] tokens = line.split( ":" );
if( tokens.length == 2 ) {
passwords.put( tokens[ 0 ], tokens[ 1 ].replace( "{SHA}", "" ).trim() );
}
}
public boolean isAuthorized( String authInfo ) {
// authInfo is provided in the header received from the client
// as a Base64 encoded string.
String credentials = new String(
Base64.getDecoder().decode( authInfo ),
Charset.forName( "UTF-8" )
);
// The string is the key:value pair username:password
String[] tokens = credentials.split( ":" );
// TODO: implement this
}
private boolean verifyPassword( String username, String password ) {
// encrypt the password, and compare it to the password stored
// in the password file (keyed by username)
// TODO: implement this - note that the encryption step is provided as a
// method, below
}
private String encryptClearPassword( String password ) {
// Encrypt the cleartext password (that was decoded from the Base64 String
// provided by the client) using the SHA-1 encryption algorithm
try {
MessageDigest mDigest = MessageDigest.getInstance( "SHA-1" );
byte[] result = mDigest.digest( password.getBytes() );
return Base64.getEncoder().encodeToString( result );
} catch( Exception e ) {
return "";
}
}
}