• bearboiblake@pawb.social
    link
    fedilink
    English
    arrow-up
    12
    arrow-down
    1
    ·
    3 hours ago

    An opportunity to share one of my favourite StackOverflow questions of all time!

    It’s well worth reading through the answers and the comments, but here’s a little sampler platter:

    This will run for a lot more than hours. Assuming it loops at 1 GHz (which it won’t - it will be a lot slower than that), it will run for 10790283070806014188970 years. Which is about 83 billion times longer than the age of the universe.

    damn - so maybe serveral threads generating guids is a better idea?

    4 threads on a quad core processor would make it run in 20 billion times the age of the universe - so yeah, that would help a lot.

    • ulterno@programming.dev
      link
      fedilink
      English
      arrow-up
      3
      ·
      edit-2
      2 hours ago

      Perhaps run 20 billion threads and you will end it by the time the universe doubles its age.

      What? I can’t set /proc/sys/kernel/threads-max to 20 billion‽
      Gotta send a bug report to the Linux kernel.

    • kionay@lemmy.world
      link
      fedilink
      English
      arrow-up
      24
      ·
      edit-2
      5 hours ago

      console.time() jots down the current time, if you do that twice and put stuff in the middle you get two times and the difference between them is how long that stuff took to do

      console.timeEnd() uses the last execution of console.time() as the starting point to work out how long the stuff took to do

      const originalUUID = crypto.randomUUID() generates a Universally Unique IDentifier, which can be thought of as a very large very random number, by use of a pseudorandom number generator

      while(stuff) evaluates the stuff for truthiness (1 + 2 = 5 would be false, 50 < 200 would be true, ‘my username starts with the letter k’ would be true) it’s typically followed by a ‘block’ of code, that is lines beginning with { and ending with }, but we don’t see that here, which means we can read while(stuff) as “keep checking if stuff is true in an endless loop, and only continue to the next line if one of the checks ends up being false

      the stuff here is creating another random UUID, and checking to see if it’s the same random number as the first one generated.

      functions like this are so incredibly random that chancing upon two executions creating the same number should be practically impossible. staggeringly impossible. If so this code should never complete, as that while check would be endless, never finding a match

      the image suggests that one such match was found in about 19 million milliseconds (a bit over 5 hours). this is probably faked, because the absurd unlikelihood of the same number being generated in so much as a single human lifetime, let alone a day, is laughable

      the imagine is faked or something is terribly wrong with their pseudorandom number generator

      • Nailbar@sopuli.xyz
        link
        fedilink
        arrow-up
        2
        ·
        33 minutes ago

        But you can’t say that it’s fake or broken just because it’s unprobable, unless there’s supposed to be some additional safe guards to prevent the same random value from repeating within a certain distance from itself.

    • Agent641@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      5 hours ago

      The function first generated a random UUID. This is a long string of random characters, used in many software systems to uniquely fingerprint things, transactions for example. In theory, you can have millions of seperate systems, each generating UUIDs all the time without ever having to worry about a collision (a collision is one or more systems generating the same UUID, therefore it being not unique anymore)

      The second line then runs UUID generation again, trying to generate an identical UUID to the one it already made. Tis is absurd because even a dmsupercomputer trying to generate identical UUIDs would take longer than the lifespan of the universe.

      The console line shows that a matching UUID was apparently found after some amount of time, which shouldn’t be possible, implying some fuckery with the random number generator.

      • devedeset@lemmy.zip
        link
        fedilink
        arrow-up
        4
        ·
        4 hours ago

        They’re basically trying to find the time to create duplicate UUIDs. UUIDs are randomly generated and assumed to be so unique and actually random across… well, everything, that no one even checks if they’re actually unique. They suggested they found one in 5 hours. The only maybe possible way I could think of to do this legitimately is to use some ridiculously powerful computer and still get very lucky.

        • 🇰 🌀 🇱 🇦 🇳 🇦 🇰 🇮 @pawb.social
          link
          fedilink
          English
          arrow-up
          1
          arrow-down
          2
          ·
          4 hours ago

          Ah! And this is why I don’t really care that much about long passwords or things of that nature. If the attack is brute force, it could still get lucky and guess it in 5 hours just like this UUID thing!

          • ClassifiedPancake@discuss.tchncs.de
            link
            fedilink
            arrow-up
            2
            ·
            3 hours ago

            The chance to get lucky and pick a long, random password is still ridiculously small. The chance to pick admin123 is ridiculously large. You see the difference?

      • Mikina@programming.dev
        link
        fedilink
        arrow-up
        5
        ·
        4 hours ago

        Aren’t UUIDs designed to prevent collisions, rather than be cryptographycally secure? Not that it’s doing a great job here :D

        Edit: Nvm, that was guid.

      • ChaoticNeutralCzech@feddit.org
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 hours ago

        That would indeed be way (quadratically) more likely but we don’t count the number of attempts but measure run time, and since comparisons (even with optimizations like insertion sort) take time, the speed difference between the two methods will be “just” a few orders of magnitude.

        • redjard@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          1
          ·
          2 hours ago

          As long as you don’t run out of memory, you can actually insert and lookup in O(1) time for a known space of values (that we have). Therefore we do get the quadratic speedup, that when dealing with bits of keysize or entropy means cutting it in half.
          Checking to get a specific uuid takes 128bit, so 2128 draws of a uuid. Putting all previous uuids into a table we expect a collision in 64bit, so 264. We also need about that much storage to contain the table, so some tens of exabytes.

      • afk_strats@lemmy.world
        link
        fedilink
        English
        arrow-up
        29
        ·
        11 hours ago

        I think this is beyond luck. This is astronomical. It’s orders of magnitude beyond what is lucky for our entire civilization to have produced in it’s entire existence

        • ProgrammingSocks@pawb.social
          link
          fedilink
          arrow-up
          11
          ·
          6 hours ago

          I’d put it down to a shit library or pseudorandomness before something happening that’s so lucky it functionally should never happen.

          • CookieOfFortune@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            5 hours ago

            Agreed. Most likely a bad implementation.

            For example, if the browser does time fuzzing, this could make the UUID less random.

    • tburkhol@lemmy.world
      link
      fedilink
      arrow-up
      67
      ·
      12 hours ago

      crypto - as in cryptography, not cryptocurrency - is just the library he’s using to generate the 128-bit random UUID. The snippet is interesting because he matched the original UUID in just over 5 hours. You’d expect to need more than 10^38 guesses to pick the same number again, which, even at 1 guess every microsecond, means something like 10^22 years.

      • Vogi@piefed.social
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        1 minute ago

        Sorry, I forgot to add the /s.

        But thank you for the calculations, it’s actually interesting :) I waas thinking about that myself, but didn’t bother to do the math.

        I thought its reminiscent of cryptomining as it also consist of guessing an arbitrary number just for fun.

        • tburkhol@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          19 minutes ago

          Internet sarcasm is hard, and lemmy has a very general audience :) I’m always happy when someone gives me an excuse to do the math I was already curious about - it’s often not worth it, for just my own curiosity, but even a sarcastic or disingenuous prompt reminds me that there’s other casually curious people out there.

      • NeatNit@discuss.tchncs.de
        link
        fedilink
        arrow-up
        32
        ·
        12 hours ago

        I wanted to downvote you for failing to pick up on the sarcasm, but then you went and did all that math that I was too lazy to do and I ended up upvoting you instead. Damn you!