# A tiny (\<1KB), fast, and cryptographically UUID (v4) generator for Crystal

**URL:** <https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774>\
**Category:** Community\
**Created:** [February 29, 2020, 10:41pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774 "2020-02-29T22:41:52Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [March 1, 2020, 12:27pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/21 "2020-03-01T12:27:45Z")

</div>

I put SLIDE parameter there so it is possible to slide different offsets if needed, Including standard 16 bytes. It loses some speed but still very fast.

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [March 1, 2020, 4:13pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/22 "2020-03-01T16:13:06Z")

</div>

I don’t think that the generator can be called cryptographically secure while it allows a user to reuse bytes from previous UUIDs.  
Caching `Random::Secure` stream doesn’t looks like a good idea too, as it makes it vulnerable to side-channel attacks.

---

<div class="post-metadata">

**Author:** ![ComputerMage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/computermage/32/719_2.png) [@ComputerMage](https://forum.crystal-lang.org/u/ComputerMage)\
**Post date:** [March 1, 2020, 4:47pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/23 "2020-03-01T16:47:37Z")

</div>

I agree. In case if you need to use UUID for something unique and totally random then it is better to use standard UUID that uses Secure::Random. If you need some unique UUID only then my approach works too.

---

<div class="post-metadata">

**Author:** ![lukeed](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/lukeed/32/756_2.png) [@lukeed](https://forum.crystal-lang.org/u/lukeed)\
**Post date:** [March 2, 2020, 5:00am UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/24 "2020-03-02T05:00:35Z")

</div>

According to [the spec](https://www.ietf.org/rfc/rfc4122.txt) (section 6) there’s no reason for it to _actually_ be secure. It was only meant as a randomized, unique string.

It’s up to you if it’s meant to be secure – but of course that’s only as good as your RNG.  
If you wanted to be secure with this implementation, you can bump the “slide” back up to `16`, yielding a fresh slice every call.

I think for the JS version I will go back to 16-byte jumps and then _also_ offer the 1-byte slide as an opt-in “insecure” variant.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [March 4, 2020, 9:03pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/25 "2020-03-04T21:03:55Z")

</div>

Unfortunately UUID’s have been used as “security capabilities” out in the wild, hence the stdlib making them all secure by default.

Couple related links:

[https://news.ycombinator.com/item?id=10631806](https://news.ycombinator.com/item?id=10631806)

> <https://github.com/crystal-lang/crystal/issues/6340#issuecomment-554653486>
>
> Hey there. I've just got a question...
> Is Crystal slower than Ruby in generating randoms?
> While playing around with Crystal I noticed that...

However for benchmarking purposes it might be nice to have faster options available, nice, thank you!

I have wondered if there’s a way to make faster cryptographically secure random’s in OS X but haven’t figured out one yet… [different algorithm seeded by urandom?]. Also crystal will get rid of fork “some day” so hopefully it’ll be at least simpler to understand at that point (or maybe they’ll keep it forever, it has some nice properties, who knows…)

---

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [March 9, 2020, 8:17pm UTC](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774/26 "2020-03-09T20:17:28Z")

</div>

You can pass in a different random number generator.

Also I thought at one point someone mentioned that UUIDs where suppose to be cryptographicly secure by the spec but after some quick searching I cannot find the comment or the documentation in the spec. Could we use a faster source of randomness that is pseudo random to increase performance?

[Previous page](https://forum.crystal-lang.org/t/a-tiny-1kb-fast-and-cryptographically-uuid-v4-generator-for-crystal/1774.md?page=1)
