Java SHA1 sha1(String input)

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

Description

sha

License

Apache License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
NullPointerException an exception

Declaration

public static String sha1(String input) throws NullPointerException 

Method Source Code


//package com.java2s;
/*//from  www. jav  a 2s  . co  m
 * Copyright 2004-2014 Aimluck,Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * 
     * @param input
     * @return
     * @throws NullPointerException
     */
    public static String sha1(String input) throws NullPointerException {
        if (input == null) {
            throw new NullPointerException("The input parameter must not be null.");
        }
        return encrypt(input, "SHA-1");
    }

    /**
     * 
     * @param input
     * @param type
     * @return
     * @throws NullPointerException
     */
    public static String encrypt(String input, String type) throws NullPointerException {
        if (input == null) {
            throw new NullPointerException("The input parameter must not be null.");
        }
        if (type == null) {
            throw new NullPointerException("The type parameter must not be null.");
        }

        try {
            MessageDigest md = MessageDigest.getInstance(type);
            md.update(input.getBytes("MS932"));
            return toHexString(md.digest());
        } catch (NoSuchAlgorithmException e) {
            // ignore
        } catch (UnsupportedEncodingException e) {
            // ignore
        }
        return null;
    }

    /**
     * 
     * @param b
     * @return
     */
    private static String toHexString(byte[] b) {
        StringBuilder hexString = new StringBuilder();
        String plainText = null;
        for (int i = 0; i < b.length; i++) {
            plainText = Integer.toHexString(0xFF & b[i]);
            if (plainText.length() < 2) {
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }
}

Related

  1. sha1(String data)
  2. SHA1(String decript)
  3. sha1(String input)
  4. sha1(String input)
  5. sha1(String input)
  6. sha1(String input)
  7. sha1(String input)
  8. sha1(String input)
  9. sha1(String input)