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 |
/** |
9aedc9
|
25 |
* Encoder zur Kennwort-Verschluesselung |
U |
26 |
* |
|
27 |
* @author Ulrich Hilger |
|
28 |
* @version 1, 08.06.2021 |
2a4fb8
|
29 |
*/ |
U |
30 |
public class Encoder { |
|
31 |
|
|
32 |
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); |
|
33 |
|
|
34 |
public byte[] encode(String password) throws NoSuchAlgorithmException { |
|
35 |
MessageDigest md = MessageDigest.getInstance("SHA-256"); |
|
36 |
return md.digest(password.getBytes()); |
|
37 |
} |
|
38 |
|
|
39 |
public String bytesToHex(byte[] bytes) { |
|
40 |
byte[] hexChars = new byte[bytes.length * 2]; |
|
41 |
for (int j = 0; j < bytes.length; j++) { |
|
42 |
int v = bytes[j] & 0xFF; |
|
43 |
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
|
44 |
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
|
45 |
} |
|
46 |
return new String(hexChars, StandardCharsets.UTF_8); |
|
47 |
} |
|
48 |
} |