/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
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);
|
}
|
}
|