commit | author | age
|
fc54b8
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.httpserver.auth.realm; |
|
7 |
|
|
8 |
import java.nio.charset.StandardCharsets; |
|
9 |
import java.security.MessageDigest; |
|
10 |
import java.security.NoSuchAlgorithmException; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author ulrich |
|
15 |
*/ |
|
16 |
public class Encoder { |
|
17 |
|
|
18 |
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); |
|
19 |
|
|
20 |
public byte[] encode(String password) throws NoSuchAlgorithmException { |
|
21 |
MessageDigest md = MessageDigest.getInstance("SHA-256"); |
|
22 |
return md.digest(password.getBytes()); |
|
23 |
} |
|
24 |
|
|
25 |
public String bytesToHex(byte[] bytes) { |
|
26 |
byte[] hexChars = new byte[bytes.length * 2]; |
|
27 |
for (int j = 0; j < bytes.length; j++) { |
|
28 |
int v = bytes[j] & 0xFF; |
|
29 |
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
|
30 |
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
|
31 |
} |
|
32 |
return new String(hexChars, StandardCharsets.UTF_8); |
|
33 |
} |
|
34 |
} |