Coder Social home page Coder Social logo

randomid's Introduction

Random ID

Inspired by Antirez's blog post, I figured I'd try to learn some Rust by implementing his strategy for creating unique IDs.

The logic is simple. On initialization, you:

  • start a counter
  • read some bytes from your machine's CSPRNG as a seed

Then, every function call:

  • calculate the HMAC-SHA1 of the concatenation of your seed and your counter's value
  • increment the counter

I really feel like this is a lot more clever than Antirez is given credit for. In terms of collision resistance, this is exceptional: HMAC-SHA1 is already about as good as it gets, plus you will get a different seed each time your code in initialized, and if that's not already enough, you concatenate the value of your counter with your seed so that the signature of each hash is different.

Consider this: the odds of two SHA1's being the same is 1 in 2^160. As far as I know, the probability of reading two of the same values from dev/urandom is 1 in 2^128. Combined, that's 1 in 2^20480. When you add that counter into the equation, even if you reinitialized your code 2^20480 times, the probability of a collision is still 2^20480. In a real application though, your odds aren't this good, plus there's the birthday paradox (except I can't remember how to calculate it lol), but still... for trying to generate random ids, this is wayyy overkill, but efficient enough that it's totally practical.

randomid's People

Contributors

bentranter avatar

Watchers

James Cloos avatar Cody Scott avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.