Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    sha1 implementation

    Scheduled Pinned Locked Moved LoPy
    2 Posts 1 Posters 982 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jobbyworld
      last edited by

      Dear pythonist,
      When do sha1 will be included in hashlib
      or
      anyone have a pure py implementation working on micropython ?
      Cheers

      1 Reply Last reply Reply Quote 0
      • J Offline
        jobbyworld
        last edited by jobbyworld

        Found myself :

        #!/usr/bin/env python
        
        import struct
        
        def leftrotate(i, n):
            return ((i << n) & 0xffffffff) | (i >> (32 - n))
        
        class SHA1(object):
            def __init__(self, data=b""):
                self.h = [
                        0x67452301,
                        0xEFCDAB89,
                        0x98BADCFE,
                        0x10325476,
                        0xC3D2E1F0
                        ]
                self.remainder = data
                self.count = 0
        
            def _add_chunk(self, chunk):
                self.count += 1
                w = list( struct.unpack(">16I", chunk) + (None,) * (80-16) )
                for i in range(16, 80):
                    n = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]
                    w[i] = leftrotate(n, 1)
                a,b,c,d,e = self.h
                for i in range(80):
                    f = None
                    k = None
                    if i < 20:
                        f = (b & c) ^ (~b & d)
                        k = 0x5A827999
                    elif i < 40:
                        f = b ^ c ^ d
                        k = 0x6ED9EBA1
                    elif i < 60:
                        f = (b & c) ^ (b & d) ^ (c & d)
                        k = 0x8F1BBCDC
                    else:
                        f = b ^ c ^ d
                        k = 0xCA62C1D6
        
                    temp = (leftrotate(a,5) + f + e + k + w[i]) % 2**32
                    e = d
                    d = c
                    c = leftrotate(b, 30)
                    b = a
                    a = temp
                self.h[0] = (self.h[0] + a) % 2**32
                self.h[1] = (self.h[1] + b) % 2**32
                self.h[2] = (self.h[2] + c) % 2**32
                self.h[3] = (self.h[3] + d) % 2**32
                self.h[4] = (self.h[4] + e) % 2**32
        
            def add(self, data):
                message = self.remainder + data
                r = len(message) % 64
                if r != 0:
                    self.remainder = message[-r:]
                else:
                    self.remainder = b""
                for chunk in range(0, len(message)-r, 64):
                    self._add_chunk( message[chunk:chunk+64] )
                return self
        
            def finish(self):
                l = len(self.remainder) + 64 * self.count
                #ll = struct.pack(">Q", l * 8)
                #FIXED
                ll2 = struct.pack('>II', (l*8)>>32,l*8) 
                self.add( b"\x80" + b"\x00" * ((55 - l) % 64) + ll2 )
                h = tuple(x for x in self.h)
                self.__init__()
                return struct.pack(">5I", *h)
        
        def main():
            s=SHA1(b"test")
            print(s.finish())
        if __name__ == '__main__':
            main()
        

        So now have websocket working !!!
        Cheers

        1 Reply Last reply Reply Quote 1

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom