class SHA1(object):
    def __init__(self, version=1):
        self.size = 160
        self.blocksize = 512
        self.wsize = 32
        assert version in (0, 1)
        self.version = version
        # set functions and constants:
        self.ft = [Ch] * 20 + [Parity] * 20 + [Maj] * 20 + [Parity] * 20
        self.K = [0x5a827999]*20 +\
                 [0x6ed9eba1]*20 +\
                 [0x8f1bbcdc]*20 +\
                 [0xca62c1d6]*20
        self.initstate()

    def initstate(self):
        H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
        self.H = [Bits(v, self.wsize) for v in H]
        self.padmethod = SHApadding(self.blocksize, self.wsize)

    def iterblocks(self, M, bitlen=None, padding=False):
        osize = self.blocksize // 8
        fmt = '>16L' if self.wsize == 32 else '>16Q'
        for B in self.padmethod.iterblocks(M, bitlen=bitlen, padding=padding):
            W = struct.unpack(fmt, B)
            yield [Bits(w, self.wsize) for w in W]

    def __call__(self, M, bitlen=None):
        self.initstate()
        return self.update(M, bitlen=bitlen, padding=True)

    def update(self, M, bitlen=None, padding=False):
        for W in self.iterblocks(M, bitlen=bitlen, padding=padding):
            a, b, c, d, e = self.H
            assert len(W) == 16
            for t in range(16, 80):
                w = rol(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16],
                        self.version)
                W.append(w)
            for r in range(80):
                T = rol(a, 5) + self.ft[r](b, c, d) + e + self.K[r] + W[r]
                e = d
                d = c
                c = rol(b, 30)
                b = a
                a = T
            self.H[0] += a
            self.H[1] += b
            self.H[2] += c
            self.H[3] += d
            self.H[4] += e
        return b''.join([pack(h, '>L') for h in self.H])
Exemple #2
0
class SHA1(object):
    def __init__(self,version=1):
        self.size = 160
        self.blocksize = 512
        self.wsize = 32
        assert version in (0,1)
        self.version = version
        # set functions and constants:
        self.ft = [Ch]*20+[Parity]*20+[Maj]*20+[Parity]*20
        self.K = [0x5a827999]*20 +\
                 [0x6ed9eba1]*20 +\
                 [0x8f1bbcdc]*20 +\
                 [0xca62c1d6]*20
        self.initstate()

    def initstate(self):
        H = [0x67452301,0xefcdab89,0x98badcfe,0x10325476,0xc3d2e1f0]
        self.H = [Bits(v,self.wsize) for v in H]
        self.padmethod = SHApadding(self.blocksize,self.wsize)

    def iterblocks(self,M,bitlen=None,padding=False):
        osize = self.blocksize/8
        fmt = '>16L' if self.wsize==32 else '>16Q'
        for B in self.padmethod.iterblocks(M,bitlen=bitlen,padding=padding):
            W = struct.unpack(fmt,B)
            yield [Bits(w,self.wsize) for w in W]

    def __call__(self,M,bitlen=None):
        self.initstate()
        return self.update(M,bitlen=bitlen,padding=True)

    def update(self,M,bitlen=None,padding=False):
        for W in self.iterblocks(M,bitlen=bitlen,padding=padding):
            a,b,c,d,e = self.H
            assert len(W)==16
            for t in range(16,80):
                w = rol(W[t-3]^W[t-8]^W[t-14]^W[t-16],self.version)
                W.append(w)
            for r in range(80):
                T = rol(a,5)+self.ft[r](b,c,d)+e+self.K[r]+W[r]
                e = d
                d = c
                c = rol(b,30)
                b = a
                a = T
            self.H[0] += a
            self.H[1] += b
            self.H[2] += c
            self.H[3] += d
            self.H[4] += e
        return ''.join([pack(h,'>L') for h in self.H])
 def initstate(self):
     t = self.outlen * 8
     if t == 224:
         H = [
             0xc1059ed8 if self.size == t else 0x8C3D37C819544DA2,
             0x367cd507 if self.size == t else 0x73E1996689DCD4D6,
             0x3070dd17 if self.size == t else 0x1DFAB7AE32FF9C82,
             0xf70e5939 if self.size == t else 0x679DD514582F9FCF,
             0xffc00b31 if self.size == t else 0x0F6D2B697BD44DA8,
             0x68581511 if self.size == t else 0x77E36F7304C48942,
             0x64f98fa7 if self.size == t else 0x3F9D85A86A1D36C8,
             0xbefa4fa4 if self.size == t else 0x1112E6AD91D692A1,
         ]
     elif t == 256:
         H = [
             0x6a09e667 if self.size == t else 0x22312194FC2BF72C,
             0xbb67ae85 if self.size == t else 0x9F555FA3C84C64C2,
             0x3c6ef372 if self.size == t else 0x2393B86B6F53B151,
             0xa54ff53a if self.size == t else 0x963877195940EABD,
             0x510e527f if self.size == t else 0x96283EE2A88EFFE3,
             0x9b05688c if self.size == t else 0xBE5E1E2553863992,
             0x1f83d9ab if self.size == t else 0x2B0199FC2C85B8AA,
             0x5be0cd19 if self.size == t else 0x0EB72DDC81C52CA2,
         ]
     elif t == 384:
         H = [
             0xcbbb9d5dc1059ed8,
             0x629a292a367cd507,
             0x9159015a3070dd17,
             0x152fecd8f70e5939,
             0x67332667ffc00b31,
             0x8eb44a8768581511,
             0xdb0c2e0d64f98fa7,
             0x47b5481dbefa4fa4,
         ]
     elif t == 512:
         H = [
             0x6a09e667f3bcc908,
             0xbb67ae8584caa73b,
             0x3c6ef372fe94f82b,
             0xa54ff53a5f1d36f1,
             0x510e527fade682d1,
             0x9b05688c2b3e6c1f,
             0x1f83d9abfb41bd6b,
             0x5be0cd19137e2179,
         ]
     self.H = [Bits(v, self.wsize) for v in H]
     self.padmethod = SHApadding(self.blocksize, self.wsize)
Exemple #4
0
 def initstate(self):
     H = [0x67452301,0xefcdab89,0x98badcfe,0x10325476,0xc3d2e1f0]
     self.H = [Bits(v,self.wsize) for v in H]
     self.padmethod = SHApadding(self.blocksize,self.wsize)
 def initstate(self):
     H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
     self.H = [Bits(v, self.wsize) for v in H]
     self.padmethod = SHApadding(self.blocksize, self.wsize)