server/password_hasher.go (1109B)
1 package main 2 3 import "golang.org/x/crypto/argon2" 4 5 type passwordRequest struct { 6 password string 7 salt []byte 8 reply chan []byte 9 } 10 11 // One worker bounds Argon2 memory without occupying a simulation owner. Inputs 12 // remain with the request until the caller receives its derived credential. 13 type passwordHasher struct { 14 requests chan passwordRequest 15 done chan struct{} 16 } 17 18 func newPasswordHasher() *passwordHasher { 19 hasher := &passwordHasher{requests: make(chan passwordRequest), done: make(chan struct{})} 20 go func() { 21 defer close(hasher.done) 22 for request := range hasher.requests { 23 request.reply <- argon2.IDKey([]byte(request.password), request.salt, 2, 19456, 1, 32) 24 } 25 }() 26 return hasher 27 } 28 29 func (server *server) browserPassword(password string, salt []byte) []byte { 30 request := passwordRequest{password: password, salt: salt, reply: make(chan []byte, 1)} 31 server.passwordHasher.requests <- request 32 return <-request.reply 33 } 34 35 // Request producers have joined before service shutdown drains the worker. 36 func (hasher *passwordHasher) stop() { 37 close(hasher.requests) 38 <-hasher.done 39 }