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

    generating random with crypto

    Scheduled Pinned Locked Moved LoPy
    8 Posts 4 Posters 2.0k Views 4 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.
    • I Offline
      iber
      last edited by

      I'm having a trouble generating a random using the crypto module.
      I'm following the documentation but I stumble upon an error.
      I'm working with the latest firmware (1.2.2.b1)

      >>> import crypto
      >>> from crypto import AES
      >>> iv = crypto.getrandbits(128)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      AttributeError: 'module' object has no attribute 'getrandbits'
      

      Seems like there's no getrandbitsmethod on the crypto module.

      A 1 Reply Last reply Reply Quote 0
      • A Offline
        abilio @iber
        last edited by

        @iber, You're totally right. The code for it was merged into master a day after the last release, but with all the restructuring of the documentation, it ended up being published before it was included in a firmware release. I will be available in today's release. Hope you find the addition useful :)

        1 Reply Last reply Reply Quote 0
        • I Offline
          iber
          last edited by

          great, looking forward to it.
          I currently moved on using a 'not so random' value ;)

          1 Reply Last reply Reply Quote 0
          • F Offline
            Feiko
            last edited by

            @ iber

            iv = uos.urandom(16)
            

            This should also work right.

            A 1 Reply Last reply Reply Quote 0
            • A Offline
              abilio @Feiko
              last edited by

              @Feiko, the result from uos.urandom comes from a pseudo random number generator. The one in crypto comes from a hardware number generator, that according to Espressif documents passes the diehard tests. I think that's probably why @iber is interested into it.

              F 1 Reply Last reply Reply Quote 0
              • F Offline
                Feiko @abilio
                last edited by

                @abilio Good to know.

                1 Reply Last reply Reply Quote 0
                • O Offline
                  obertuckerz112
                  last edited by

                  Do you specifically need it?

                  A 1 Reply Last reply Reply Quote 0
                  • A Offline
                    abilio @obertuckerz112
                    last edited by

                    uos.urandom is based on a linear feedback shift register, that is seeded with a number derived from the MAC address. The values will "look" like random for a whole period, but still easily predictable.

                    crypto.getrandbits is based on a hardware random number generator that gathers it's bits from random events (noise) received by the WiFi and Bluetooth radios. This source, according to the Espressif, passes randomness tests.

                    @obertuckerz112, needed for writing a few AES examples? probably not. Needed for real life applications? The experts in cryptography say that you need a good source (as in unpredictable) of random numbers for things like the initialization vector (IV). Clearly, uos.urandom is not.

                    In order to allow people to construct the best possible applications, we hence provide access to the hardware random number generator getrandbits as an usable source and recommend the people to use it instead of uos.urandom for cryptography purposes.

                    1 Reply Last reply Reply Quote 0

                    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