欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sha1 go实现

程序员文章站 2022-06-05 08:53:35
...

//补位
func add(x string) []byte{
var bytedata =[]byte(x)//将字符串转为字节数组
var bytelen = len(bytedata)//字节长
var bitlen = bytelen*8//位长
//FillSize = 448 - ((mLen * 8) % 512); //计算需填充的bit数
var needbit = 448 - (bitlen % 512)
var needbyte =needbit/8
var buf []byte
//如果小于448位
if bitlen<=448 {
for _,v:= range bytedata{
buf=append(buf,v)//将原数据加入切片
}
buf=append(buf,0x80)//补1
for i:=bytelen+1;i<56 ;i++ {
buf=append(buf,0x00)//补0
}
//填充后64位,为数据长度

	var ooo  [64]byte
	bytelen <<= 3
	for i := uint(0); i < 8; i++ {
		ooo[i] = byte(bytelen >> (8 * i))
	}
	for _,v:=range ooo [0:8] {
		buf=append(buf,v)
	}
} else if bitlen>448 {
	for _, v := range bytedata {
		buf = append(buf, v)
	}
	//补01
	buf = append(buf, 0x10)

	for i := 0; i < needbyte-1; i++ {
		buf = append(buf, 0x00)
	}
	//补长度

	var ooo  [64]byte
	bytelen <<= 3
	for i := uint(0); i < 8; i++ {
		ooo[i] = byte(bytelen >> (8 * i))
	}
	for _,v:=range ooo [0:8] {
		buf=append(buf,v)
	}
}
return buf

}

//分组
func divGroup_512(x []byte)([][]byte){
var t1 []byte
var t2 [][]byte
var len= len(x)
var num =len/64
for i:=1;i<=num ;i++ {//共分为nunm个512分组
for k:=i64-64;k<i64 ;k++ {//每一512分组的值
t1=append(t1,x[k])
}
t2=append(t2,t1)
t1=nil
}
return t2

}
//将每一512位分组分为16个小分组,,每组32位(4字节)
var child0 []byte
var child11 []byte
var child22 [][]byte
var child33 [][][]byte
func divgroup_16(x [][]byte)[][][]byte{
var len = len(x)

for i:=0;i<len ;i++  {
		child0=x[i]//第i个512分组
		//每个512分组内分为16小组
	for k:=1;k<=16 ;k++  {
		for j:=k*4-4;j<k*4 ;j++ {
			child11=append(child11,child0[j])//每四个为一小组
		}
		child22=append(child22,child11)
		child11=nil
	}
	child33=append(child33,child22)
}
return child33

}
//扩充为80组
func expand80(x [][][]byte) [][][]byte {
var group80 [][][]byte
var yyy [][]byte
var uuu [][]byte
/*

fmt.Print(group80)*/
for i:=0;i< len(x);i++  {
	yyy=x[i]//16分组
	//fmt.Println(yyy)

	//将每一个16分组扩充为80组
	for k:=0;k<16 ;k++  {
		uuu=append(uuu,yyy[k])
	}
	//fmt.Println(uuu)
	for k:=16;k<80 ;k++  {
		t1:=BytesToInt1(uuu[k-3])
		t2:=BytesToInt1(uuu[k-8])
		t3:=BytesToInt1(uuu[k-14])
		t4:=BytesToInt1(uuu[k-16])
		t5:=t1^t2^t3^t4
		t6:=t5<<1
		uuu=append(uuu,IntToBytes2(t6))
	}
	group80=append(group80,uuu)
	uuu=nil
}
return group80

}

//循环
var(
A uint=0x67452301
B uint=0xEFCDAB89
C uint=0x98BADCFE
D uint=0x10325476
E uint=0xC3D2E1F0
A1=A
B1=B
C1=C
D1=D
E1=E
k1 uint=0x5A827999
K2 uint=0x6ED9EBA1
k3 uint=0x8F188CDC
k4 uint=0xCA62C1D6
)
func trans( x [][][]byte)[][]byte {
var ddd [][]byte
for i:=0;i< len(x);i++ {
ddd=x[i]//一个80分组
// 第一轮
for i:=0;i<20 ;i++ {

		A=A<<5+F1(B,C,D)+E+BytesToInt1(ddd[i])+k1
		B=A1
		C=B1<<30
		D=C1
		E=D1
	}
	//第二轮
	for i:=20;i<40 ;i++  {
		A=A<<5+F2(B,C,D)+E+BytesToInt1(ddd[i])+K2
		B=A1
		C=B1<<30
		D=C1
		E=D1
	}
	//第三轮
	for i:=40;i<60 ;i++  {
		A=A<<5+F3(B,C,D)+E+BytesToInt1(ddd[i])+k3
		B=A1
		C=B1<<30
		D=C1
		E=D1
	}
	//fmt.Println(A)
	//第四轮
	for i:=60;i<80 ;i++  {
		A=A<<5+F4(B,C,D)+E+BytesToInt1(ddd[i])+k4
		B=A1
		C=B1<<30
		D=C1
		E=D1
	}
	A+=A1
	B+=B1
	C+=C1
	D+=D1
	E+=E1
}
var result []uint
result=append(result,A)
result=append(result,B)
result=append(result,C)
result=append(result,D)
result=append(result,E)

resultt:=op(result)

return resultt

}

//四轮计算函数

//0-19
func F1(B,C,D uint) uint {
var T1=B&C
var T2=^B&D
return T1|T2
}
//20-39
func F2(B,C,D uint)uint {
return BCD
}
//40-59
func F3(B,C,D uint)uint {
var t1 =B&C
var t2 =B&D
var t3 =C&D
return t1|t2|t3
}
//60-79
func F4(B,C,D uint)uint {
return BCD
}

相关标签: go sha1