/*
|
http-realm - Authentication Extensions to jdk.httpserver
|
Copyright (C) 2021 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.httpserver.auth.realm;
|
|
import java.nio.charset.StandardCharsets;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
/**
|
*
|
* @author ulrich
|
*/
|
public class Encoder {
|
|
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
|
|
public byte[] encode(String password) throws NoSuchAlgorithmException {
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
return md.digest(password.getBytes());
|
}
|
|
public String bytesToHex(byte[] bytes) {
|
byte[] hexChars = new byte[bytes.length * 2];
|
for (int j = 0; j < bytes.length; j++) {
|
int v = bytes[j] & 0xFF;
|
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
|
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
|
}
|
return new String(hexChars, StandardCharsets.UTF_8);
|
}
|
}
|