> I'd like to challenge the whole setup:
> If you need *unique* ids why not just use an var id uint64 and id++ approach?
> Or do you need "unpredictable" ids? Is a tiny chance of id collisions okay?
The tiny chance of collisions is the type of id I want. I am making unique references to an arbitrary number of pieces of data. This data could be running over multiple processes. So it is the tiny chance of collision that I am interested in.
> I am not a cryptographer but I don't think hashing 20 new random bytes is pretty much overkill:
> 1. generating a stream of random bytes with a cryptographically secure png
> 2. those are hashed with a cryptographically secure one way function
> Which type of attacks do you intend to prevent with this twofold work?
The hash of 20 new random bytes is because 20 is a nice round number. I really don't know what a good value is here. I am just using this hash an id to access data. The hashing function has the advantage of giving a consistent output, which makes it much better for an id. (At least this is what I intended to do. I am not trying to defend against any attacks here.)
> You are not using SHA-1 not really as a hash function: You are just taking
> 20 bytes input and convert them output 160 bit output (which is the same).
> I think hashing a uniformly distributed source will not reduce the probability of collisions.
Aha. Now this is a mistake on my part. What is the correct way of using a sha-1 hash as a id?
> You stated that you need these ids "fast" but your code will read as many
> bytes from /dev/urandom as you need for your ids. Arguably this happens
> in different goroutines so it can be done in parallel but the problem will
> be too low entropy in the kernels entropy pool to read from /dev/urandom.
Well I wasn't trying to be clever here. Just generate the ids before I needed them. To be honest this comes from a little project I am doing to teach myself go. This seemed like a logical place to put a useful goroutine. (It does make my tests run a little quicker)
> Why not seed AES from crypto/rand and generate the ids from AES in
> CTR mode? Most likely this will be much faster.
I hadn't thought of this. My starting point for these ids was to look around and see what other software uses for ids. (Document stores etc). They use seem to use either sha-1 or sha-256. Would the AES in CTR mode by as good?
> One advise: Never do your own algos on any cryptographic stuff.
definitely good advice that I always try to live up to (except when I don't)
> V.
On Wednesday, 27 March 2013 23:27:38 UTC, Ciaran Doherty wrote:
Hi,
I have a newbie question here, hopefully some wise insight can be shed on this.
I have a need to get a lot of unique id very quickly, after reading the docs, I was told that the best way to get unique ids was to the crytpo package but that this would run slow. So what I have done is use channel as a buffer and have an infinite loop in a goroutine to keep this channel filled. The upside is that I 'always' have quick access to a new unique id. The down side are I have no idea whether or not this a good idea, or how to safely shut the process after start this loop.
Any insights/feedback will be much appreciated. Thank you (code below)
import (
"crypto/rand"
"crypto/sha1"
"io"
"sync"
)
func genRandNum() *[]byte {
count := 1024
rand_store := make([]byte, count)
io.ReadFull(rand.Reader, rand_store)
return &rand_store
}
var id_buffer chan []byte = make(chan []byte, 10)
var gen_ids_once sync.Once
func bufferNewIds() {
for {
rand_store := genRandNum()
hasher := sha1.New()
hasher.Write(*rand_store)
id_buffer <- hasher.Sum(nil)[0:20]
}
}