Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Title:        efa - elektronisches Fahrtenbuch fr Ruderer
 * Copyright:    Copyright (c) 2001-2011 by Nicolas Michael
 * Website:      http://efa.nmichael.de/
 * License:      GNU General Public License v2
 *
 * @author Nicolas Michael
 * @version 2
 */

import java.io.*;

import java.security.*;

public class Main {
    public static String getSHA(String z) {
        if (z == null || z.length() == 0) {
            return null;
        }
        return getSHA(z.getBytes());
    }

    public static String getSHA(byte[] message) {
        return getSHA(message, message.length);
    }

    public static String getSHA(byte[] message, int length) {
        if (message == null || length <= 0) {
            return null;
        }
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(message, 0, length);
            byte[] output = md.digest();
            String s = "";
            for (int i = 0; i < output.length; i++) {
                s += hexByte(output[i] + 128);
            }
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getSHA(File f) {
        return getSHA(f, (int) f.length());
    }

    public static String getSHA(File f, int length) {
        if (!f.isFile() || length <= 0) {
            return null;
        }
        FileInputStream f1;
        byte[] buf = new byte[length];
        int n;
        try {
            f1 = new FileInputStream(f);
            n = f1.read(buf, 0, length);
            f1.close();
        } catch (IOException e) {
            return null;
        }
        return getSHA(buf);
    }

    public static String hexByte(int i) {
        return getHexDigit(i / 16) + "" + getHexDigit(i % 16);
    }

    public static String hexByte(byte b) {
        int i = b;
        if (i < 0) {
            i += 256;
        }
        return getHexDigit(i / 16) + "" + getHexDigit(i % 16);
    }

    public static char getHexDigit(int i) {
        if (i < 10) {
            return (char) (i + '0');
        } else {
            return (char) ((i - 10) + 'A');
        }
    }
}