Utils.java Source code

Java tutorial

Introduction

Here is the source code for Utils.java

Source

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

public class Utils {
    public static String digest(String token) throws GeneralSecurityException, IOException {
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        return byteArrayToHexStr(sha1.digest(token.getBytes("UTF-8")));
    }

    private static String byteArrayToHexStr(byte[] data) {
        char[] chars = new char[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            byte current = data[i];
            int hi = (current & 0xF0) >> 4;
            int lo = current & 0x0F;
            chars[2 * i] = (char) (hi < 10 ? ('0' + hi) : ('A' + hi - 10));
            chars[2 * i + 1] = (char) (lo < 10 ? ('0' + lo) : ('A' + lo - 10));
        }
        return new String(chars);
    }
}