Ejemplo n.º 1
0
        }// end of SHA1

        /// <summary>
        /// SHA1算法的填充分组方法,将this对象的data按照sha1需要进行分组
        /// </summary>
        /// <returns>分组后的数据</returns>
        private byte[][] SHA1FillGroups()
        {
            //划定分组
            byte[][] groups;
            if ((data.Length % (512 / 8)) >= (448 / 8))
            {
                groups = new byte[data.Length / (512 / 8) + 2][];
            }
            else
            {
                groups = new byte[data.Length / (512 / 8) + 1][];
            }

            //  填充分组
            for (int i = 0; i < groups.Length; ++i)
            {
                groups[i] = new byte[512 / 8];
                //填充分组i,当剩余数据不足以填满一个512bit分组时执行else
                if (data.Length - i * (512 / 8) >= 512 / 8)
                {
                    Array.Copy(data, 512 / 8 * i, groups[i], 0, 512 / 8);
                }
                else
                {
                    Array.Copy(data, 512 / 8 * i, groups[i], 0, data.Length - i * (512 / 8));
                    //原文末尾添加1
                    groups[i][data.Length - i * (512 / 8)] = 0x80;
                    //若该分组不够64bit记录原文长度, 则再填充一个分组
                    if ((data.Length - i * (512 / 8)) >= (448 / 8))
                    {
                        groups[++i] = new byte[512 / 8];
                    }
                    //最后一个分组的最后8个字节填充数据长度(以bit为单位)
                    Array.Copy(MyBitConverter.GetBytes(Convert.ToUInt64(data.Length * 8)), 0, groups[i], 512 / 8 - 8, 8);
                }
            }
            return(groups);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// SHA1算法计算指定分组的w0~w79数据的方法
 /// </summary>
 /// <param name="group">需要计算的分组</param>
 /// <returns>计算得到的w0~w79</returns>
 private byte[][] SHA1W0_W79(byte[] group)
 {
     byte[][] w0_w79 = new byte[80][];
     //将输入分组的512bit作为w0_w15
     for (int i = 0; i < 16; ++i)
     {
         w0_w79[i] = new byte[32 / 8];
         Array.Copy(group, i * 4, w0_w79[i], 0, 32 / 8);
     }
     for (int i = 16; i < 80; ++i)
     {
         //异或求值后循环左移1bit
         UInt32 wt =
             MyBitConverter.ToUInt32(w0_w79[i - 16]) ^
             MyBitConverter.ToUInt32(w0_w79[i - 14]) ^
             MyBitConverter.ToUInt32(w0_w79[i - 8]) ^
             MyBitConverter.ToUInt32(w0_w79[i - 3]);
         wt = (wt << 1) | (wt >> 31);
         //对W_t进行赋值
         w0_w79[i] = MyBitConverter.GetBytes(wt);
     }
     return(w0_w79);
 }