Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2015 FeedHenry Ltd, All Rights Reserved.
 *
 * Please refer to your contract with FeedHenry for the software license agreement.
 * If you do not have a contract, you do not have a license to use this software.
 */

import java.security.MessageDigest;

public class Main {
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String generateHash(String pText) throws Exception {
        String hashValue;
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(pText.getBytes("ASCII"));
        hashValue = encodeHex(md.digest());
        return hashValue;
    }

    private static String encodeHex(byte[] pData) {
        int l = pData.length;

        char[] out = new char[l << 1];

        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & pData[i]) >>> 4];
            out[j++] = DIGITS[0x0F & pData[i]];
        }

        return new String(out);
    }
}