Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 304|回复: 0

[算法学习]SHA-1摘要算法源码(java版)

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-6 00:03:21 | 显示全部楼层 |阅读模式
    1. 来源:http://blog.csdn.net/zyg158/archive/2007/06/26/1667531.aspx
    2.                         
    3. public class SHA1 {
    4.     private final int[] abcde = {
    5.             0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
    6.         };
    7.     // 摘要数据存储数组
    8.     private int[] digestInt = new int[5];
    9.     // 计算过程中的临时数据存储数组
    10.     private int[] tmpData = new int[80];
    11.     // 计算sha-1摘要
    12.     private int process_input_bytes(byte[] bytedata) {
    13.         // 初试化常量
    14.         System.arraycopy(abcde, 0, digestInt, 0, abcde.length);
    15.         // 格式化输入字节数组,补10及长度数据
    16.         byte[] newbyte = byteArrayFormatData(bytedata);
    17.         // 获取数据摘要计算的数据单元个数
    18.         int MCount = newbyte.length / 64;
    19.         // 循环对每个数据单元进行摘要计算
    20.         for (int pos = 0; pos < MCount; pos++) {
    21.             // 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中
    22.             for (int j = 0; j < 16; j++) {
    23.                 tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));
    24.             }
    25.             // 摘要计算函数
    26.             encrypt();
    27.         }
    28.         return 20;
    29.     }
    30.     // 格式化输入字节数组格式
    31.     private byte[] byteArrayFormatData(byte[] bytedata) {
    32.         // 补0数量
    33.         int zeros = 0;
    34.         // 补位后总位数
    35.         int size = 0;
    36.         // 原始数据长度
    37.         int n = bytedata.length;
    38.         // 模64后的剩余位数
    39.         int m = n % 64;
    40.         // 计算添加0的个数以及添加10后的总长度
    41.         if (m < 56) {
    42.             zeros = 55 - m;
    43.             size = n - m + 64;
    44.         } else if (m == 56) {
    45.             zeros = 63;
    46.             size = n + 8 + 64;
    47.         } else {
    48.             zeros = 63 - m + 56;
    49.             size = (n + 64) - m + 64;
    50.         }
    51.         // 补位后生成的新数组内容
    52.         byte[] newbyte = new byte[size];
    53.         // 复制数组的前面部分
    54.         System.arraycopy(bytedata, 0, newbyte, 0, n);
    55.         // 获得数组Append数据元素的位置
    56.         int l = n;
    57.         // 补1操作
    58.         newbyte[l++] = (byte) 0x80;
    59.         // 补0操作
    60.         for (int i = 0; i < zeros; i++) {
    61.             newbyte[l++] = (byte) 0x00;
    62.         }
    63.         // 计算数据长度,补数据长度位共8字节,长整型
    64.         long N = (long) n * 8;
    65.         byte h8 = (byte) (N & 0xFF);
    66.         byte h7 = (byte) ((N >> 8) & 0xFF);
    67.         byte h6 = (byte) ((N >> 16) & 0xFF);
    68.         byte h5 = (byte) ((N >> 24) & 0xFF);
    69.         byte h4 = (byte) ((N >> 32) & 0xFF);
    70.         byte h3 = (byte) ((N >> 40) & 0xFF);
    71.         byte h2 = (byte) ((N >> 48) & 0xFF);
    72.         byte h1 = (byte) (N >> 56);
    73.         newbyte[l++] = h1;
    74.         newbyte[l++] = h2;
    75.         newbyte[l++] = h3;
    76.         newbyte[l++] = h4;
    77.         newbyte[l++] = h5;
    78.         newbyte[l++] = h6;
    79.         newbyte[l++] = h7;
    80.         newbyte[l++] = h8;
    81.         return newbyte;
    82.     }
    83.     private int f1(int x, int y, int z) {
    84.         return (x & y) | (~x & z);
    85.     }
    86.     private int f2(int x, int y, int z) {
    87.         return x ^ y ^ z;
    88.     }
    89.     private int f3(int x, int y, int z) {
    90.         return (x & y) | (x & z) | (y & z);
    91.     }
    92.     private int f4(int x, int y) {
    93.         return (x << y) | x >>> (32 - y);
    94.     }
    95.     // 单元摘要计算函数
    96.     private void encrypt() {
    97.         for (int i = 16; i <= 79; i++) {
    98.             tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14] ^
    99.                     tmpData[i - 16], 1);
    100.         }
    101.         int[] tmpabcde = new int[5];
    102.         for (int i1 = 0; i1 < tmpabcde.length; i1++) {
    103.             tmpabcde[i1] = digestInt[i1];
    104.         }
    105.         for (int j = 0; j <= 19; j++) {
    106.             int tmp = f4(tmpabcde[0], 5) +
    107.                 f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
    108.                 tmpData[j] + 0x5a827999;
    109.             tmpabcde[4] = tmpabcde[3];
    110.             tmpabcde[3] = tmpabcde[2];
    111.             tmpabcde[2] = f4(tmpabcde[1], 30);
    112.             tmpabcde[1] = tmpabcde[0];
    113.             tmpabcde[0] = tmp;
    114.         }
    115.         for (int k = 20; k <= 39; k++) {
    116.             int tmp = f4(tmpabcde[0], 5) +
    117.                 f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
    118.                 tmpData[k] + 0x6ed9eba1;
    119.             tmpabcde[4] = tmpabcde[3];
    120.             tmpabcde[3] = tmpabcde[2];
    121.             tmpabcde[2] = f4(tmpabcde[1], 30);
    122.             tmpabcde[1] = tmpabcde[0];
    123.             tmpabcde[0] = tmp;
    124.         }
    125.         for (int l = 40; l <= 59; l++) {
    126.             int tmp = f4(tmpabcde[0], 5) +
    127.                 f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
    128.                 tmpData[l] + 0x8f1bbcdc;
    129.             tmpabcde[4] = tmpabcde[3];
    130.             tmpabcde[3] = tmpabcde[2];
    131.             tmpabcde[2] = f4(tmpabcde[1], 30);
    132.             tmpabcde[1] = tmpabcde[0];
    133.             tmpabcde[0] = tmp;
    134.         }
    135.         for (int m = 60; m <= 79; m++) {
    136.             int tmp = f4(tmpabcde[0], 5) +
    137.                 f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
    138.                 tmpData[m] + 0xca62c1d6;
    139.             tmpabcde[4] = tmpabcde[3];
    140.             tmpabcde[3] = tmpabcde[2];
    141.             tmpabcde[2] = f4(tmpabcde[1], 30);
    142.             tmpabcde[1] = tmpabcde[0];
    143.             tmpabcde[0] = tmp;
    144.         }
    145.         for (int i2 = 0; i2 < tmpabcde.length; i2++) {
    146.             digestInt[i2] = digestInt[i2] + tmpabcde[i2];
    147.         }
    148.         for (int n = 0; n < tmpData.length; n++) {
    149.             tmpData[n] = 0;
    150.         }
    151.     }
    152.     // 4字节数组转换为整数
    153.     private int byteArrayToInt(byte[] bytedata, int i) {
    154.         return ((bytedata[i] & 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16) |
    155.         ((bytedata[i + 2] & 0xff) << 8) | (bytedata[i + 3] & 0xff);
    156.     }
    157.     // 整数转换为4字节数组
    158.     private void intToByteArray(int intValue, byte[] byteData, int i) {
    159.         byteData[i] = (byte) (intValue >>> 24);
    160.         byteData[i + 1] = (byte) (intValue >>> 16);
    161.         byteData[i + 2] = (byte) (intValue >>> 8);
    162.         byteData[i + 3] = (byte) intValue;
    163.     }
    164.     // 将字节转换为十六进制字符串
    165.     private static String byteToHexString(byte ib) {
    166.         char[] Digit = {
    167.                 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
    168.                 "D", "E", "F"
    169.             };
    170.         char[] ob = new char[2];
    171.         ob[0] = Digit[(ib >>> 4) & 0X0F];
    172.         ob[1] = Digit[ib & 0X0F];
    173.         String s = new String(ob);
    174.         return s;
    175.     }
    176.     // 将字节数组转换为十六进制字符串
    177.     private static String byteArrayToHexString(byte[] bytearray) {
    178.         String strDigest = "";
    179.         for (int i = 0; i < bytearray.length; i++) {
    180.             strDigest += byteToHexString(bytearray[i]);
    181.         }
    182.         return strDigest;
    183.     }
    184.     // 计算sha-1摘要,返回相应的字节数组
    185.     public byte[] getDigestOfBytes(byte[] byteData) {
    186.         process_input_bytes(byteData);
    187.         byte[] digest = new byte[20];
    188.         for (int i = 0; i < digestInt.length; i++) {
    189.             intToByteArray(digestInt[i], digest, i * 4);
    190.         }
    191.         return digest;
    192.     }
    193.     // 计算sha-1摘要,返回相应的十六进制字符串
    194.     public String getDigestOfString(byte[] byteData) {
    195.         return byteArrayToHexString(getDigestOfBytes(byteData));
    196.     }
    197.     public static void main(String[] args) {
    198.         String data = "1";
    199.         System.out.println(data);
    200.         String digest = new SHA1().getDigestOfString(data.getBytes());
    201.         System.out.println(digest);
    202.     }
    203. }
    复制代码
    运行结果:
    C:        est>java SHA1
    1
    356A192B7913B04C54574D18C28D46E6395428AB


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/6/000321312.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 11:12 , Processed in 0.357246 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表