How to use ss method in Selene

Best Python code snippet using selene_python

pypy_sha256.py

Source:pypy_sha256.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# SHA256 compression function implementation below is a verbatim copy of PyPy's implementation from4# https://bitbucket.org/pypy/pypy/raw/f1f064b3faf1e012f7a9a9ab08f18074637ebe8a/lib_pypy/_sha256.py .5#6# It is licensed under the MIT license and copyright PyPy Copyright holders 2003-20157# See https://bitbucket.org/pypy/pypy/src/tip/LICENSE for the full copyright notice.8#9SHA_BLOCKSIZE = 6410SHA_DIGESTSIZE = 3211def new_shaobject():12 return {13 'digest': [0]*8,14 'count_lo': 0,15 'count_hi': 0,16 'data': [0]* SHA_BLOCKSIZE,17 'local': 0,18 'digestsize': 019 }20ROR = lambda x, y: (((x & 0xffffffff) >> (y & 31)) | (x << (32 - (y & 31)))) & 0xffffffff21Ch = lambda x, y, z: (z ^ (x & (y ^ z)))22Maj = lambda x, y, z: (((x | y) & z) | (x & y))23S = lambda x, n: ROR(x, n)24R = lambda x, n: (x & 0xffffffff) >> n25Sigma0 = lambda x: (S(x, 2) ^ S(x, 13) ^ S(x, 22))26Sigma1 = lambda x: (S(x, 6) ^ S(x, 11) ^ S(x, 25))27Gamma0 = lambda x: (S(x, 7) ^ S(x, 18) ^ R(x, 3))28Gamma1 = lambda x: (S(x, 17) ^ S(x, 19) ^ R(x, 10))29def sha_transform(sha_info):30 W = []31 d = sha_info['data']32 for i in range(0,16):33 W.append( (d[4*i]<<24) + (d[4*i+1]<<16) + (d[4*i+2]<<8) + d[4*i+3])34 for i in range(16,64):35 W.append( (Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]) & 0xffffffff )36 ss = sha_info['digest'][:]37 def RND(a,b,c,d,e,f,g,h,i,ki):38 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i];39 t1 = Sigma0(a) + Maj(a, b, c);40 d += t0;41 h = t0 + t1;42 return d & 0xffffffff, h & 0xffffffff43 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],0,0x428a2f98);44 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],1,0x71374491);45 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],2,0xb5c0fbcf);46 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],3,0xe9b5dba5);47 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],4,0x3956c25b);48 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],5,0x59f111f1);49 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],6,0x923f82a4);50 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],7,0xab1c5ed5);51 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],8,0xd807aa98);52 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],9,0x12835b01);53 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],10,0x243185be);54 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],11,0x550c7dc3);55 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],12,0x72be5d74);56 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],13,0x80deb1fe);57 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],14,0x9bdc06a7);58 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],15,0xc19bf174);59 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],16,0xe49b69c1);60 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],17,0xefbe4786);61 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],18,0x0fc19dc6);62 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],19,0x240ca1cc);63 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],20,0x2de92c6f);64 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],21,0x4a7484aa);65 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],22,0x5cb0a9dc);66 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],23,0x76f988da);67 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],24,0x983e5152);68 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],25,0xa831c66d);69 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],26,0xb00327c8);70 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],27,0xbf597fc7);71 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],28,0xc6e00bf3);72 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],29,0xd5a79147);73 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],30,0x06ca6351);74 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],31,0x14292967);75 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],32,0x27b70a85);76 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],33,0x2e1b2138);77 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],34,0x4d2c6dfc);78 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],35,0x53380d13);79 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],36,0x650a7354);80 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],37,0x766a0abb);81 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],38,0x81c2c92e);82 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],39,0x92722c85);83 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],40,0xa2bfe8a1);84 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],41,0xa81a664b);85 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],42,0xc24b8b70);86 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],43,0xc76c51a3);87 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],44,0xd192e819);88 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],45,0xd6990624);89 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],46,0xf40e3585);90 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],47,0x106aa070);91 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],48,0x19a4c116);92 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],49,0x1e376c08);93 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],50,0x2748774c);94 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],51,0x34b0bcb5);95 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],52,0x391c0cb3);96 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],53,0x4ed8aa4a);97 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],54,0x5b9cca4f);98 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],55,0x682e6ff3);99 ss[3], ss[7] = RND(ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],56,0x748f82ee);100 ss[2], ss[6] = RND(ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],57,0x78a5636f);101 ss[1], ss[5] = RND(ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],ss[5],58,0x84c87814);102 ss[0], ss[4] = RND(ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],ss[4],59,0x8cc70208);103 ss[7], ss[3] = RND(ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],ss[3],60,0x90befffa);104 ss[6], ss[2] = RND(ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],ss[2],61,0xa4506ceb);105 ss[5], ss[1] = RND(ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],ss[1],62,0xbef9a3f7);106 ss[4], ss[0] = RND(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6],ss[7],ss[0],63,0xc67178f2);107 dig = []108 for i, x in enumerate(sha_info['digest']):109 dig.append( (x + ss[i]) & 0xffffffff )110 sha_info['digest'] = dig111def sha_init():112 sha_info = new_shaobject()113 sha_info['digest'] = [0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19]114 sha_info['count_lo'] = 0115 sha_info['count_hi'] = 0116 sha_info['local'] = 0117 sha_info['digestsize'] = 32118 return sha_info119def sha224_init():120 sha_info = new_shaobject()121 sha_info['digest'] = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]122 sha_info['count_lo'] = 0123 sha_info['count_hi'] = 0124 sha_info['local'] = 0125 sha_info['digestsize'] = 28126 return sha_info127def sha_update(sha_info, buffer):128 if isinstance(buffer, str):129 raise TypeError("Unicode strings must be encoded before hashing")130 count = len(buffer)131 buffer_idx = 0132 clo = (sha_info['count_lo'] + (count << 3)) & 0xffffffff133 if clo < sha_info['count_lo']:134 sha_info['count_hi'] += 1135 sha_info['count_lo'] = clo136 sha_info['count_hi'] += (count >> 29)137 if sha_info['local']:138 i = SHA_BLOCKSIZE - sha_info['local']139 if i > count:140 i = count141 # copy buffer142 sha_info['data'][sha_info['local']:sha_info['local']+i] = buffer[buffer_idx:buffer_idx+i]143 count -= i144 buffer_idx += i145 sha_info['local'] += i146 if sha_info['local'] == SHA_BLOCKSIZE:147 sha_transform(sha_info)148 sha_info['local'] = 0149 else:150 return151 while count >= SHA_BLOCKSIZE:152 # copy buffer153 sha_info['data'] = list(buffer[buffer_idx:buffer_idx + SHA_BLOCKSIZE])154 count -= SHA_BLOCKSIZE155 buffer_idx += SHA_BLOCKSIZE156 sha_transform(sha_info)157 # copy buffer158 pos = sha_info['local']159 sha_info['data'][pos:pos+count] = buffer[buffer_idx:buffer_idx + count]160 sha_info['local'] = count161def sha_final(sha_info):162 lo_bit_count = sha_info['count_lo']163 hi_bit_count = sha_info['count_hi']164 count = (lo_bit_count >> 3) & 0x3f165 sha_info['data'][count] = 0x80;166 count += 1167 if count > SHA_BLOCKSIZE - 8:168 # zero the bytes in data after the count169 sha_info['data'] = sha_info['data'][:count] + ([0] * (SHA_BLOCKSIZE - count))170 sha_transform(sha_info)171 # zero bytes in data172 sha_info['data'] = [0] * SHA_BLOCKSIZE173 else:174 sha_info['data'] = sha_info['data'][:count] + ([0] * (SHA_BLOCKSIZE - count))175 sha_info['data'][56] = (hi_bit_count >> 24) & 0xff176 sha_info['data'][57] = (hi_bit_count >> 16) & 0xff177 sha_info['data'][58] = (hi_bit_count >> 8) & 0xff178 sha_info['data'][59] = (hi_bit_count >> 0) & 0xff179 sha_info['data'][60] = (lo_bit_count >> 24) & 0xff180 sha_info['data'][61] = (lo_bit_count >> 16) & 0xff181 sha_info['data'][62] = (lo_bit_count >> 8) & 0xff182 sha_info['data'][63] = (lo_bit_count >> 0) & 0xff183 sha_transform(sha_info)184 dig = []185 for i in sha_info['digest']:186 dig.extend([ ((i>>24) & 0xff), ((i>>16) & 0xff), ((i>>8) & 0xff), (i & 0xff) ])187 return ''.join([chr(i) for i in dig])188class sha256(object):189 digest_size = digestsize = SHA_DIGESTSIZE190 block_size = SHA_BLOCKSIZE191 def __init__(self, s=None):192 self._sha = sha_init()193 if s:194 sha_update(self._sha, s)195 def update(self, s):196 sha_update(self._sha, s)197 def digest(self):198 return sha_final(self._sha.copy())[:self._sha['digestsize']]199 def hexdigest(self):200 return ''.join(['%.2x' % ord(i) for i in self.digest()])201 def copy(self):202 new = sha256.__new__(sha256)203 new._sha = self._sha.copy()204 return new205class sha224(sha256):206 digest_size = digestsize = 28207 def __init__(self, s=None):208 self._sha = sha224_init()209 if s:210 sha_update(self._sha, s)211 def copy(self):212 new = sha224.__new__(sha224)213 new._sha = self._sha.copy()214 return new215def test():216 a_str = "just a test string"217 assert 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' == sha256().hexdigest()218 assert 'd7b553c6f09ac85d142415f857c5310f3bbbe7cdd787cce4b985acedd585266f' == sha256(a_str).hexdigest()219 assert '8113ebf33c97daa9998762aacafe750c7cefc2b2f173c90c59663a57fe626f21' == sha256(a_str*7).hexdigest()220 s = sha256(a_str)221 s.update(a_str)222 assert '03d9963e05a094593190b6fc794cb1a3e1ac7d7883f0b5855268afeccc70d461' == s.hexdigest()223if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful