Home > other >  Questions about sha256 algorithm
Questions about sha256 algorithm

Time:09-15

On the Internet to find the two, an encrypted the original Chinese results would be different, and can not be greater than or equal to 64 characters, another can but also can not be greater than or equal to 64 characters in Chinese, where is the problem

Method 1

The import struct
The import binascii
The import hashlib

# 64 constant K
X71374491 x428a2f98 _K=(0, 0, 0 xb5c0fbcf, 0 xe9b5dba5,
X923f82a4 x3956c25b 0, 0 x59f111f1, 0, 0 xab1c5ed5,
X243185be xd807aa98 0, 0 x12835b01, 0, 0 x550c7dc3,
X9bdc06a7 x72be5d74 0, 0 x80deb1fe, 0, 0 xc19bf174,
X0fc19dc6 xe49b69c1 0, 0 xefbe4786, 0, 0 x240ca1cc,
X5cb0a9dc x2de92c6f 0, 0 x4a7484aa, 0, 0 x76f988da,
Xb00327c8 x983e5152 0, 0 xa831c66d, 0, 0 xbf597fc7,
X06ca6351 xc6e00bf3 0, 0 xd5a79147, 0, 0 x14292967,
X4d2c6dfc x27b70a85 0, 0 x2e1b2138, 0, 0 x53380d13,
X81c2c92e x650a7354 0, 0 x766a0abb, 0, 0 x92722c85,
Xc24b8b70 xa2bfe8a1 0, 0 xa81a664b, 0, 0 xc76c51a3,
Xf40e3585 xd192e819 0, 0 xd6990624, 0, 0 x106aa070,
X2748774c x19a4c116 0, 0 x1e376c08, 0, 0 x34b0bcb5,
X5b9cca4f x391c0cb3 0, 0 x4ed8aa4a, 0, 0 x682e6ff3,
X84c87814 x748f82ee 0, 0 x78a5636f, 0, 0 x8cc70208,
Xbef9a3f7 x90befffa 0, 0 xa4506ceb, 0, 0 xc67178f2)

# initialization cache
Xbb67ae85 x6a09e667 _H=(0, 0, 0 x3c6ef372, 0 xa54ff53a,
X1f83d9ab x510e527f 0, 0 x9b05688c, 0, 0 x5be0cd19)


# define sha1_256 class
The class sha_256:

# input parameters for clear
Def __init__ (self, m=None) :
# initialization plaintext
The self. The buffer=b '
# input plaintext length
The self. The counter=0
Self. H=_H
Self. K=_K

If m:
The self. The update (m)

# define cycle moves to the right method
Def rotr (self, x, y) :
Return ((x & gt;> Y) | (x & lt; <(32 - y))) & amp; 0 XFFFFFFFF

# definition of operation method of the single group
Def operate (self, c) :
# define the length of 64 empty list w
W=[0] * 64
# converts a single packet to 16 32-bit words, and populate the first 16 w list as
W/0:16=struct. Unpack ('! 16 l ', c)

# filled w list after 48
For I in range (16, 64) :
S0=self. Rotr (w/I - 15, 7) ^. Self rotr (w/I - 15, 18) ^ (w [15] I - & gt;> 3)
S1=self. Rotr (w/I - 2, 17) ^. Self rotr (w/I - 2, 19) ^ (w/I - 2 & gt;> 10)
W=[I] [16] I - (w + s0 + w [I] + s1) & amp; 0 XFFFFFFFF

A, b, c, d, e, f, g, h=self. H

# 64 steps iterative operation on
For I in range (64) :
S0=self. Rotr (a, 2) ^. Self rotr (a, 13) ^. Self rotr (a, 22)
Maj=(a & amp; B) ^ (a & amp; C) ^ (b & amp; C)
T2=s0 + maj,
S1=self. Rotr (e, 6) ^. Self rotr (e, 11) ^. Self rotr (e, 25)
Ch=(e & amp; F) ^ ((~ e) & amp; G)
T1=ch of h + s1 + + self. [I] [I] + w K

H=g
G=f
F=e
E=(d + t1) & amp; 0 XFFFFFFFF
D=c
C=b
B=a
A=(t1 + t2) & amp; 0 XFFFFFFFF

# update cache
Self. H=[(x + y) & amp; 0 XFFFFFFFF for x, y in the zip (self. J H, [a, b, c, d, e, f, g, H])]

# define update cache grouping method of N
Def update (self, m) :
If not m:
Return

# get clear
The self. The buffer=m
# get clear length
The self. The counter=len (m)
# calculation length of plaintext said after 64
Length=struct. Pack ('! Q ', int (self. Counter * 8))
# for N - 1 packet hash process before
While len (self. Buffer) & gt;=64:
Self. _operate (self. Buffer [: 64])
The self. The buffer=self. The buffer/64:

The first N # fill in untreated group to 512 or 1024, and the hash process
Mdi=self. Counter % 64
# if the NTH packet is less than 56, the filling to 512
If mdi & lt; 56:
Padlen=55 - mdi
Self. Buffer +=(b '\ x80' + '\ x00 (b * padlen) + length)
Self. Operate (self. Buffer)
# otherwise, fill to a 1024 - bit
The else:
Padlen=119 - mdi
Self. Buffer +=(b '\ x80' + '\ x00 (b * padlen) + length)
For I in range (2) :
Self. Operate (self) buffer [I * 64: (I + 1) * 64])

# output clear summary, byte string type
Def digest (self) :
Return struct. Pack ('! 8 l ', * self. H)

# output proclaimed in writing to, hex string type
Def hexdigest (self) :
Return binascii. Hexlify (self. Digest ()). The decode ()

Saz=input (' please enter the encrypted string ')

If __name__=="__main__" :
Print (sha_256 ((saz encode (' utf-8))). The digest ()) # algorithm implementation above
Print (hashlib. Sha256 ((saz encode (' utf-8))). The digest ()) # bring hashlib library
Print (sha_256 ((saz encode (' utf-8))). Hexdigest ()) # algorithm implementation above
Print (hashlib. Sha256 ((saz encode (' utf-8))). Hexdigest ()) # bring hashlib library







Method 2
The import struct
Def out_hex (list1) :
For I in list1:
Print (" 8 x % % I)
Print (" \ n ")

Def rotate_left (a k) :
32 k=k %
Return ((a & lt; (32 - k))
Def rotate_right (a k) :
32 k=k %
Return (((a & gt;> K) & amp; 0 XFFFFFFFF) | ((a & amp; 0 XFFFFFFFF) & lt; <(32 k))) & amp; 0 XFFFFFFFF

Def rotate_shift (a k) :
32 k=k %
Return ((a & gt;> K) & amp; 0 XFFFFFFFF);

Def P_0 (X) :
Return (rotate_right (X, 7)) ^ (rotate_right (X, 18)) ^ (rotate_shift (X, 3))

Def P_1 (X) :
Return (rotate_right (X, 17)) ^ (rotate_right (X, 19)) ^ (rotate_shift (X, 10))

IV="0 x6a09e667 x9b05688c x510e527f xa54ff53a x3c6ef372 xbb67ae85 0 0 0 0 0 0 x1f83d9ab 0 x5be0cd19"

IV=IV. Replace (" 0 x ", "")
IV=int (IV. Replace (" ", ""), 16)


K="" "xb5c0fbcf x428a2f98 0, 0 x71374491, 0, 0, xe9b5dba5 x3956c25b 0, 0 x59f111f1, 0 x923f82a4, 0 xab1c5ed5,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related