Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.security.MessageDigest;

public class Main {
    private static final String SHA1 = "SHA-1";

    public static String generateSHA1Hash(String textToBeHashed) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
            messageDigest.update(textToBeHashed.getBytes());
            byte[] sha1hash = messageDigest.digest();
            return getHexString(sha1hash);
        } catch (Exception e) {
            throw new RuntimeException("Couldn't generate SHA-1 hash for " + textToBeHashed);
        }
    }

    private static String getHexString(byte[] data) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            stringBuffer.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1));
        }
        return stringBuffer.toString();
    }
}