Java SHA1 sha1Hash(String input)

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

Description

TODO: move to better algorithm?

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String sha1Hash(String input) 

Method Source Code

//package com.java2s;
/*//from ww  w .  j  a  v a 2 s .c  o  m
 * Copyright (c) 2016. Sten Martinez
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static MessageDigest _sha1Digest = null;

    /**
     * TODO: move to better algorithm?
     * @param input
     * @return
     */
    public static String sha1Hash(String input) {
        if (_sha1Digest == null) {
            try {
                _sha1Digest = MessageDigest.getInstance("SHA-1");
            } catch (NoSuchAlgorithmException e) {
                // this will not fail.
                e.printStackTrace();
                return null;
            }
        }

        byte[] digest = _sha1Digest.digest(input.getBytes());

        StringBuilder sb = new StringBuilder();

        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }

        return sb.toString();
    }
}

Related

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