代码如下,请自取

/**
* @description: SHA256算法加密
* @author: luolei
* @Date: 2022-10-31 17:16
*/
public class SHA256 {
// 用long模拟32位无符号数据
// 前32位0,后32位1的数字,控制数字在32位,4294967295
final static long OVER = 0xFFFFFFFFL;
// 前8个质数的平方根的小数
// long[] h = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
static long h0 = 0x6a09e667L;
static long h1 = 0xbb67ae85L;
static long h2 = 0x3c6ef372L;
static long h3 = 0xa54ff53aL; static long h4 = 0x510e527fL;
static long h5 = 0x9b05688cL;
static long h6 = 0x1f83d9abL;
static long h7 = 0x5be0cd19L; // 前64个质数的立方根的小数
static long[] k = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; public static String getSha256(String in) {
StringBuilder stringBuilder = toBinary(in);
String string = addZeroTo512(stringBuilder);
return cycleCalculation(string);
} // 1. 将输入的字符串转换为二进制in
// string的长度限制在2^32
private static StringBuilder toBinary(String in) {
StringBuilder stringBuilder = new StringBuilder();
char[] inArray = in.toCharArray();
for (int i = 0; i < inArray.length; i++) {
// 还有前导的0
String binary = Integer.toBinaryString(inArray[i]);
int count0 = 8 - binary.length();
for (int j = 0; j < count0; j++) {
stringBuilder.append("0");
}
stringBuilder.append(binary);
}
return stringBuilder;
} // 2. 将输入的二进制的长度补齐到512的倍数,原长度l + 1(默认1位1) + k(补齐的0) + 64(64为二进制表示的输入字符串的长度)
private static String addZeroTo512(StringBuilder binaryIn) { int l = binaryIn.length();
int k = 959 - (l % 512);
if (k > 512) {
k = k - 512;
}
// 默认先添加1
binaryIn.append("1");
// 添加k个0
for (int i = 0; i < k; i++) {
binaryIn.append("0");
}
// 添加64位的源数据长度
String lengthBinary = Integer.toBinaryString(l);
int k2 = 64 - lengthBinary.length();
for (int i = 0; i < k2; i++) {
binaryIn.append("0");
}
binaryIn.append(lengthBinary); return binaryIn.toString();
} // 核心:循环计算 /**
* @param binaryIn 通过补位后的源数据
* @return
*/
private static String cycleCalculation(String binaryIn) {
// 1. 按照512的长度分成n个消息块
int n = binaryIn.length() / 512;
for (int i = 0; i < n; i++) {
// 对于每一个块
// 2. 每个消息块分成16个32位的“字”
String[] wString = new String[16];
for (int j = 0; j < 16; j++) {
wString[j] = binaryIn.substring(32 * j, 32 + 32 * j);
}
// 3. 将16个字扩充为64个字,转换方法
long[] w = new long[64];
for (int j = 0; j < wString.length; j++) {
// 将二进制的string转为10进制的long
w[j] = Long.parseLong(wString[j], 2);
}
// s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor(w[i-15] rightshift 3)
// s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor(w[i-2] rightshift 10)
// w[i] := w[i-16] + s0 + w[i-7] + s1
for (int j = 16; j < 64; j++) {
long s0 = ((rightRotate(w[j - 15], 7)) ^ rightRotate(w[j - 15], 18) ^ (rightShift(w[j - 15], 3))) & OVER;
long s1 = ((rightRotate(w[j - 2], 17)) ^ (rightRotate(w[j - 2], 19)) ^ (rightShift(w[j - 2], 10))) & OVER;
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) & OVER;
}
// 4. hash初始化
long a = h0;
long b = h1;
long c = h2;
long d = h3; long e = h4;
long f = h5;
long g = h6;
long h = h7;
// 5. 64次循环
for (int j = 0; j < 64; j++) {
// s0 := (a rightrotate 2) xor (a rightrotate 13) xor(a rightrotate 22)
// maj := (a and b) xor (a and c) xor(b and c)
// t2 := s0 + maj
// s1 := (e rightrotate 6) xor (e rightrotate 11) xor(e rightrotate 25)
// ch := (e and f) xor ((not e) and g)
// t1 := h + s1 + ch + k[i] + w[i]
// h := g
// g := f
// f := e
// e := d + t1
// d := c
// c := b
// b := a
// a := t1 + t2 long s0 = (rightRotate(a, 2) ^ (rightRotate(a, 13)) ^ (rightRotate(a, 22))) & OVER;
long maj = ((a & b) ^ (a & c) ^ (b & c)) & OVER;
long t2 = (s0 + maj) & OVER;
long s1 = ((rightRotate(e, 6)) ^ (rightRotate(e, 11)) ^ (rightRotate(e, 25))) & OVER;
long ch = ((e & f) ^ ((~e) & g)) & OVER;
long t1 = (h + s1 + ch + k[j] + w[j]) & OVER;
h = g;
g = f;
f = e;
e = (d + t1) & OVER;
d = c;
c = b;
b = a;
a = (t1 + t2) & OVER;
} h0 = (h0 + a) & OVER;
h1 = (h1 + b) & OVER;
h2 = (h2 + c) & OVER;
h3 = (h3 + d) & OVER;
h4 = (h4 + e) & OVER;
h5 = (h5 + f) & OVER;
h6 = (h6 + g) & OVER;
h7 = (h7 + h) & OVER;
}
// 把h0到h7拼起来就是所需要的值了。
return getHash(h0) + getHash(h1) + getHash(h2) + getHash(h3) + getHash(h4) + getHash(h5) + getHash(h6) + getHash(h7);
} // 把long型的数字转换为字符串,只取后32位的数字,不足则前补0,前面多余的则舍去
private static String getHash(long h) {
String hash = Long.toHexString(h);
StringBuilder result = new StringBuilder();
if (hash.length() > 8) {
result.append(hash.substring(hash.length() - 8, hash.length()));
} else {
int count0 = 8 - hash.length();
for (int i = 0; i < count0; i++) {
result.append("0");
}
result.append(hash);
}
return result.toString();
} /**
* 右旋n位,循环右移n位,对于32位无符号数字而言,末尾的数字被移到开头
* 用long模拟32位无符号数字,末尾被移出的数字替换到开头,相当于左移了
*
* @param x
* @param n
* @return
*/
private static long rightRotate(long x, int n) {
long wei = (0 << n) - 1; // 这个操作有点疑问
x = ((wei & (x & OVER)) << (32 - n)) | (x & OVER) >> n;
return x;
} /**
* 按位右移n位,末尾的数字舍去,前面补0,相当于>>>,无符号右移
*
* @param x
* @param n
* @return
*/
private static long rightShift(long x, int n) {
// return (x&0xFFFFFFFFL)>>n;
return (x & OVER) >>> n;
}
}

SHA256算法加密工具类的更多相关文章

  1. App开发流程之加密工具类

    科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...

  2. java 加密工具类(MD5、RSA、AES等加密方式)

    1.加密工具类encryption MD5加密 import org.apache.commons.codec.digest.DigestUtils; /** * MD5加密组件 * * @autho ...

  3. Java AES 加密工具类

    package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...

  4. 加密工具类 - CryptoUtils.java

    加密工具类,包含MD5,BASE64,SHA,CRC32的加密与解密方法. 源码如下:(点击下载  - CryptoUtils.java.commons-io-2.4.jar.commons-code ...

  5. android开发MD5加密工具类(一)

    MD5加密工具类整理: package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.secu ...

  6. wemall app商城源码android开发MD5加密工具类

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供 ...

  7. c# 加密工具类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...

  8. java MD5Utils 加密工具类

    package com.sicdt.library.core.utils; import java.io.File; import java.io.FileInputStream; import ja ...

  9. spring自带的MD5加密工具类

    Spring 自带的md5加密工具类,本来打算自己找一个工具类的,后来想起来Spring有自带的,就翻了翻 //导入包import org.springframework.util.DigestUti ...

  10. 封装各种生成唯一性ID算法的工具类

    /** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 ( ...

随机推荐

  1. 2023-07-20:假设一共有M个车库,编号1~M,时间点从早到晚是从1~T, 一共有N个记录,每一条记录如下{a, b, c}, 表示一辆车在b时间点进入a车库,在c时间点从a车库出去, 一共有K

    2023-07-20:假设一共有M个车库,编号1 ~ M,时间点从早到晚是从1 ~ T, 一共有N个记录,每一条记录如下{a, b, c}, 表示一辆车在b时间点进入a车库,在c时间点从a车库出去, ...

  2. ListView选中获取数据并弹出菜单项

    前言 作为一名Android小白,我在编写过程中,使用ListView列表,想要使用他来完成长按弹出菜单选项,并且还要进行事件操作,经过百度编程的经历后,终于成功完成.在此附上这块比较完整的代码,理论 ...

  3. 安装centos系统,硬盘检测报错:修改BIOS为 Legacy

    进bios,将模式修改为legacy. 硬盘使用 MBR 分区,需要用 Legacy BIOS 启动,而不是 UEFI BIOS . 至于为什么安装的时候会报错? 可能是系统有这方面限制.也可能是别的 ...

  4. JFrame一些基础小知识

    JFrame.setLocationRelativeTo方法 JFrame.setLocationRelativeTo()是一个Java Swing中的方法,它用于将窗口居中显示在屏幕上. 当你调用该 ...

  5. ubuntu下安装mysqlclient报错

    输入以下代码: 1 解决方法: 2 sudo apt-get install libmysqlclient-dev 3 4 再次安装: 5 pip3 install mysqlclient 文章链接: ...

  6. 从MybatisPlus回归Mybatis

    从MybatisPlus回归Mybatis 之前写项目一直习惯使用MyBatisPlus,单表查询很方便:两张表也很方便,直接业务层处理两张表的逻辑.但什么都图方便只会害了你. 但连接的表比较复杂的时 ...

  7. dotnet SemanticKernel 入门 自定义变量和技能

    本文将告诉大家如何在 SemanticKernel 框架内定义自定义的变量和如何开发自定义的技能 本文属于 SemanticKernel 入门系列博客,更多博客内容请参阅我的 博客导航 自定义变量是一 ...

  8. 了解JAVA内存模型(JMM)

    1.概述 我们常说的JMM指的是Java内存模型(Java Memory Model,JMM),主要用于控制Java程序解决线程间如何通信和数据同步,JMM规范了多线程访问共享内存时的 可见性.有序性 ...

  9. 制作一个内部的 zabbix-agent 快速部署脚本

    下载官方的基础 agent 部署包 官方地址:点击到达 curl -O https://cdn.zabbix.com/zabbix/binaries/stable/5.0/5.0.36/zabbix_ ...

  10. web组态可视化编辑器

    随着工业智能制造的发展,工业企业对设备可视化.远程运维的需求日趋强烈,传统的单机版组态软件已经不能满足越来越复杂的控制需求,那么实现web组态可视化界面成为了主要的技术路径. 行业痛点 对于软件服务商 ...