zoukankan      html  css  js  c++  java
  • SHA256加密算法封装[我的代码库]

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

    public class SHA256Test {
        public static final String ALGORITHM = "SHA-256";

        public static String SHA256Encrypt(String orignal) {
            MessageDigest md = null;
            try {
                md = MessageDigest.getInstance(ALGORITHM);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            if (null != md) {
                byte[] origBytes = orignal.getBytes();
                md.update(origBytes);
                byte[] digestRes = md.digest();
                String digestStr = getDigestStr(digestRes);
                return digestStr;
            }

            return null;
        }

        private static String getDigestStr(byte[] origBytes) {
            String tempStr = null;
            StringBuilder stb = new StringBuilder();
            for (int i = 0; i < origBytes.length; i++) {
                // System.out.println("and by bit: " + (origBytes[i] & 0xff));
                // System.out.println("no and: " + origBytes[i]);
                // System.out.println("---------------------------------------------");
                // 这里按位与是为了把字节转整时候取其正确的整数,java中一个int是4个字节
                // 如果origBytes[i]最高位为1,则转为int时,int的前三个字节都被1填充了
                tempStr = Integer.toHexString(origBytes[i] & 0xff);
                if (tempStr.length() == 1) {
                    stb.append("0");
                }
                stb.append(tempStr);

            }
            return stb.toString();
        }

        public static void main(String[] args) {
            System.out.println(SHA256Encrypt("AAaa11"));
        }

    }

  • 相关阅读:
    Prime Time使用
    cache与MMU与总线仲裁
    IUS tcl cmd
    CPU cache
    generated clock
    PL301 matrix内部模块
    014-数据结构-树形结构-基数树、Patricia树、默克尔树、梅克尔帕特里夏树( Merkle Patricia Tree, MPT)
    001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充
    013-数据结构-树形结构-决策树
    012-数据结构-树形结构-哈希树[hashtree]、字典树[trietree]、后缀树
  • 原文地址:https://www.cnblogs.com/leipei2352/p/2614131.html
Copyright © 2011-2022 走看看