Java SHA1 sha1(String string)

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

Description

sha

License

Apache License

Declaration

public static final String sha1(String string) 

Method Source Code


//package com.java2s;
/*/*  ww  w .  j  av  a2 s  .  c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.*;
import java.math.BigInteger;
import java.nio.file.Path;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String sha1(String string) {
        return calcHash(string, "SHA-1");
    }

    public static final String sha1(File file) throws FileNotFoundException, IOException {
        return calcHash(new FileInputStream(file), "SHA-1");
    }

    public static final String sha1(Path file) throws FileNotFoundException, IOException {
        return sha1(file.toFile());
    }

    public static final String sha1(InputStream inStream) throws IOException {
        return calcHash(inStream, "SHA-1");
    }

    public static final String sha1(byte[] bytes) {
        return calcHash(bytes, "SHA-1");
    }

    private static String calcHash(String string, String algorithm) {
        try {
            return calcHash(string.getBytes("UTF-8"), algorithm);
        } catch (UnsupportedEncodingException e) {
            return calcHash(string.getBytes(), algorithm);
        }
    }

    private static String calcHash(byte[] bytes, String algorithm) {
        try {
            MessageDigest m = MessageDigest.getInstance(algorithm);
            m.update(bytes);
            return new BigInteger(1, m.digest()).toString(16);
        } catch (NoSuchAlgorithmException ex) {
            // this should not happen
            return "";
        }
    }

    private static String calcHash(InputStream input, String algorithm) throws IOException {
        try {
            final MessageDigest md = MessageDigest.getInstance(algorithm);
            final DigestInputStream dis = new DigestInputStream(input, md);
            try {
                byte[] buff = new byte[4096];
                // just read to get the Digest filled...
                while (dis.read(buff) > 0) {
                    // nop
                }
                return new BigInteger(1, md.digest()).toString(16);
            } finally {
                dis.close();
            }
        } catch (NoSuchAlgorithmException e) {
            // this should not happen
            return "";
        }
    }
}

Related

  1. sha1(String str)
  2. SHA1(String str)
  3. sha1(String str)
  4. sha1(String str)
  5. SHA1(String str)
  6. sha1(String string)
  7. SHA1(String strs)
  8. sha1(String text)
  9. SHA1(String text)