Java SHA1 sha1(String s)

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

Description

sha

License

Apache License

Declaration

public static String sha1(String s) 

Method Source Code

//package com.java2s;
/**/*  w  w w  . j a  v  a2s.  co m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   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.lang.reflect.Array;

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

public class Main {
    public static String sha1(String s) {
        MessageDigest mDigest;
        try {
            mDigest = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] result = mDigest.digest(s.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }

    public static String toString(Object o, String nullValue) {
        if (o == null)
            return nullValue;
        try {
            if (o.getClass().getDeclaredMethod("toString") != null)
                return o.toString();
        } catch (NoSuchMethodException | SecurityException e1) {
        }

        if (o.getClass().isArray()) {
            StringBuilder sb = new StringBuilder();
            int l = Array.getLength(o);
            sb.append("[");
            for (int i = 0; i < l; i++) {
                if (i > 0)
                    sb.append(",");
                sb.append(toString(Array.get(o, i), nullValue));
            }
            sb.append("]");
            return sb.toString();
        }
        if (o instanceof Iterable) {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            for (Object e : ((Iterable) o)) {
                if (sb.length() > 1)
                    sb.append(",");
                sb.append(toString(e, nullValue));
            }
            sb.append("]");
            return sb.toString();
        }

        return String.valueOf(o);
    }

    public static String toString(Object o) {
        return toString(o, "null");
    }

    public static String toString(CharSequence chars) {
        if (chars instanceof String)
            return (String) chars;

        StringBuilder sb = new StringBuilder(chars.length());
        for (int i = 0; i < chars.length(); i++)
            sb.append(chars.charAt(i));
        return sb.toString();
    }

    public static String toString(CharSequence chars, int start, int end) {
        if (chars instanceof String)
            return ((String) chars).substring(start, end);

        StringBuilder sb = new StringBuilder(end - start);
        for (int i = start; i < end; i++)
            sb.append(chars.charAt(i));
        return sb.toString();
    }
}

Related

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