// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package updatespeed;

import java.io.*;
import java.security.*;

import javax.mail.internet.*;

public class Hash {
    private static char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static String toHex(byte[] ba) {
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < ba.length; i++) {
            sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
            sb.append(hexdigit[ba[i] & 0x0F]);
        }
        return sb.toString();
    }
    private static String toBase64(byte[] b) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream b64os = MimeUtility.encode(baos, "base64");
        b64os.write(b);
        b64os.close();
        return new String(baos.toByteArray(), Config.CHARSET);
    }
    private static MessageDigest md;
    static {
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    public static String hash(String pw) throws Exception {
        return toBase64(md.digest(pw.getBytes(Config.CHARSET)));
    }
}
