Java SHA1 sha1(String s)

Here you can find the source of sha1(String s)

Description

sha

License

Open Source License

Declaration

public static String sha1(String s) 

Method Source Code


//package com.java2s;
/*//from w w w  .ja v a2 s  .  c o  m
 * utils-java8
 * Copyright (C) 2014 Raffaele Ragni
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String DIGEST_SHA1 = "SHA1";

    public static String sha1(String s) {
        return digest(s, DIGEST_SHA1);
    }

    public static String digest(String source, String algorythm) {
        if (source == null || source.length() == 0) {
            return source;
        }

        try {
            StringBuilder result = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorythm);
            byte[] bytes = md.digest(source.getBytes("UTF-8"));
            for (byte b : bytes) {
                String bb = "0" + Integer.toHexString(b);
                result.append(bb.substring(bb.length() - 2));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            return null;
        }
    }
}

Related

  1. sha1(String message)
  2. sha1(String param)
  3. sha1(String raw)
  4. sha1(String s)
  5. sha1(String s)
  6. sha1(String s)
  7. sha1(String s)
  8. sha1(String s)
  9. sha1(String s)