commit | author | age
|
2a4fb8
|
1 |
/* |
U |
2 |
http-realm - Authentication Extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.auth.realm; |
|
19 |
|
|
20 |
import java.nio.charset.StandardCharsets; |
|
21 |
import java.security.MessageDigest; |
|
22 |
import java.security.NoSuchAlgorithmException; |
|
23 |
|
|
24 |
/** |
|
25 |
* |
|
26 |
* @author ulrich |
|
27 |
*/ |
|
28 |
public class Encoder { |
|
29 |
|
|
30 |
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); |
|
31 |
|
|
32 |
public byte[] encode(String password) throws NoSuchAlgorithmException { |
|
33 |
MessageDigest md = MessageDigest.getInstance("SHA-256"); |
|
34 |
return md.digest(password.getBytes()); |
|
35 |
} |
|
36 |
|
|
37 |
public String bytesToHex(byte[] bytes) { |
|
38 |
byte[] hexChars = new byte[bytes.length * 2]; |
|
39 |
for (int j = 0; j < bytes.length; j++) { |
|
40 |
int v = bytes[j] & 0xFF; |
|
41 |
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
|
42 |
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
|
43 |
} |
|
44 |
return new String(hexChars, StandardCharsets.UTF_8); |
|
45 |
} |
|
46 |
} |