Java SHA1 sha1Hash(String input)

Here you can find the source of sha1Hash(String input)

Description

sha Hash

License

Open Source License

Declaration

public static String sha1Hash(String input) 

Method Source Code

//package com.java2s;
/**/*from w w w  .  j  a v  a  2 s  .  c  o m*/
 * This file is part of Jaybukkit.
 *
 * Jaybukkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jaybukkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jaybukkit.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.security.MessageDigest;
import java.util.logging.Logger;

public class Main {
    private static final Logger logger = Logger.getLogger("Minecraft");

    public static String sha1Hash(String input) {
        try {
            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
            sha1Digest.update(input.getBytes());
            return byteArrayToString(sha1Digest.digest());
        } catch (Exception e) {
            logger.severe(e.getMessage());
        }

        return "";
    }

    static String byteArrayToString(byte[] array) {
        StringBuilder builder = new StringBuilder();

        for (byte next : array) {
            byte lower = (byte) (next & 0x0F);

            byte higher = (byte) ((next & 0xF0) >>> 4);

            builder.append(getHexChar(higher));
            builder.append(getHexChar(lower));
        }

        return builder.toString();
    }

    static char getHexChar(byte convertible) {
        switch (convertible) {
        case 10:
            return 'a';
        case 11:
            return 'b';
        case 12:
            return 'c';
        case 13:
            return 'd';
        case 14:
            return 'e';
        case 15:
            return 'f';
        default:
            return Integer.toString(convertible).charAt(0);
        }
    }
}

Related

  1. sha1Hash(File file)
  2. sha1Hash(final String tohash)
  3. sha1Hash(InputStream in)
  4. sha1Hash(String content)
  5. sha1Hash(String input)
  6. sha1hash(String key)
  7. sha1Hash(String s)
  8. sha1Hash(String source)
  9. sha1Hash(String text)