Java SHA256 sha256(String plainText)

Here you can find the source of sha256(String plainText)

Description

sha

License

Apache License

Declaration

public static String sha256(String plainText) 

Method Source Code

//package com.java2s;
/*/*from   ww w. jav  a  2s. c o  m*/
 *  Copyright 2015 the original author or authors. 
 *  @https://github.com/scouter-project/scouter
 *
 *  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.security.MessageDigest;

public class Main {
    public static String sha256(String plainText) {
        String salt = "qwertyuiop!@#$%^&*()zxcvbnm,.";
        String sha256Text = null;
        if (plainText != null) {
            try {
                MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
                sha256.update(salt.getBytes());
                byte[] byteArray = plainText.getBytes();
                byte[] sha256Bytes = sha256.digest(byteArray);
                StringBuffer buf = new StringBuffer();

                for (int i = 0; i < sha256Bytes.length; i++) {
                    if ((sha256Bytes[i] & 0xff) < 0x10) {
                        buf.append("0");
                    }
                    buf.append(Long.toString(sha256Bytes[i] & 0xff, 16));
                }

                sha256Text = buf.toString();
            } catch (Throwable t) {
                return plainText;
            }
        }
        return sha256Text;
    }
}

Related

  1. sha256(String data)
  2. sha256(String Input)
  3. SHA256(String input)
  4. sha256(String message)
  5. sha256(String password, String salt)
  6. sha256(String raw)
  7. sha256(String s)
  8. sha256(String src)
  9. sha256(String srcStr)