org.icelib.Hash.java Source code

Java tutorial

Introduction

Here is the source code for org.icelib.Hash.java

Source

/**
 * Copyright (C) 2014-2016 Emerald Icemoon (emerald.icemoon@gmail.com)
 * 
 * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
 */
package org.icelib;

import java.security.MessageDigest;
import java.util.Formatter;

import org.apache.commons.codec.digest.DigestUtils;

public class Hash {

    public final static String AUTH_SALT = "key";

    // Byte sequence generated by http://www.random.org/
    // WARNING: Changing this will invalidate the keys of any existing accounts.
    public final static String serverSalt = "W7UDynoSUED5zbdA5ldiSlxV9DX0Fssl8VdhRUZWkG6D6XRGETXKRVdJMI2LAGvZyawgHkD26OIKDxz4"
            + "xrWdol3RQp8JJ79lmCRQHfMBVa0jXyUaZSxq23lRNBjzMBKymxXmfZ9ugfuvIbV9NpF4b7K5GgACcWy7"
            + "Xwz2P4wLYknUtIFLLZcQqFhlrcXRpsbtmc4JzKJqKPrsLlKby0PF3EGhSOLWfXO0bRrvpvXoxtuDnh2Z"
            + "F6GUqW4X9JeFvMbLV8mcMm1i37SNPMwILzy1JqbYTeVtJLDozCVftVo4dBQJcgb9CMLGeCxO5DT2Xw5X"
            + "gcyJgYXFFwRatjdPyLs4boM3aCTyWsNyNFfHArHati2mFGBS5CXqfazIhau6hyMlkxmGR3WUxioCpzq4"
            + "H7q6usn8oSETobwEqsAqdVeMk06621glbeV9yofr7SapSTnp2xXSyklDcr5hOiEoFdCLkePQQoAEAtEx"
            + "jKLu0aTpKMRmysq5kTiS";

    public static String hash(String text, String username) {
        return hash(text, username, AUTH_SALT);
    }

    public static String hash(String text, String username, boolean server) {
        return hash(text, username, AUTH_SALT, server);
    }

    public static String hash(String text, String username, String salt) {
        return hash(text, username, salt, false);
    }

    public static String hash(String text, String username, String salt, boolean server) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            byte[] hashedPw = instance.digest(text.getBytes("ASCII"));
            final String authString = username + ":" + encode(hashedPw) + ":" + salt;
            String encoded = encode(instance.digest(authString.getBytes("ASCII")));
            if (server) {
                instance.reset();
                instance.update(encoded.getBytes("ASCII"));
                instance.update(salt.getBytes("ASCII"));
                encoded = encode(instance.digest());
            }
            return encoded;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String generateSalt(String in) {
        try {
            return DigestUtils.md5Hex((in + serverSalt + "\0").getBytes("ASCII"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String encode(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        Formatter formatter = new Formatter(sb);
        for (byte b : bytes) {
            int val = (int) b & 0xff;
            formatter.format("%02x", val);
        }
        return sb.toString();
    }
}