Java SHA sha2(String content)

Here you can find the source of sha2(String content)

Description

sha

License

Apache License

Declaration

public static String sha2(String content) 

Method Source Code

//package com.java2s;
/*/*from  w w w. j  av  a  2s  . c  o m*/
 * Copyright 2013 Rui Sun (SteveSunCanada@gmail.com)
 *
 * The Netty Project 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.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String sha2(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            byte[] thedigest = md.digest(content.getBytes());

            char chars[] = new char[32 * 2];
            for (int i = 0; i < thedigest.length; i++) {
                chars[i * 2] = hexDigits[thedigest[i] >>> 4 & 0xf];
                chars[i * 2 + 1] = hexDigits[thedigest[i] & 0xf];
            }

            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. SHA(String s)
  2. sha(String source)
  3. sha(String str)
  4. sha(String strPlain)
  5. sha(String text, String encoding)
  6. sha2(String input)
  7. sha2(String password)
  8. sha2(String... data)
  9. sha5Encode(byte[] content)