Skip to content

Instantly share code, notes, and snippets.

@ghthor
Created October 1, 2012 06:24
Show Gist options
  • Save ghthor/3809802 to your computer and use it in GitHub Desktop.
Save ghthor/3809802 to your computer and use it in GitHub Desktop.
Messing with crypto/rand generating uint64's
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
. "github.com/ghthor/gospec/src/gospec"
"github.com/ghthor/gospec/src/gospec"
"log"
"math"
"math/big"
)
func NewMaxUint64() *big.Int {
var i big.Int
buf := bytes.NewBuffer(make([]byte, 0, 8))
err := binary.Write(buf, binary.BigEndian, uint64(math.MaxUint64))
if err != nil {
log.Fatal(err)
}
i.SetBytes(buf.Bytes())
return &i
}
func DescribePasswordHashing(c gospec.Context) {
c.Specify("create a big.Int with math.MaxUint64", func() {
maxUint64 := NewMaxUint64()
c.Expect(maxUint64.String(), Equals, fmt.Sprintf("%d", uint64(math.MaxUint64)))
var i uint64
err := binary.Read(bytes.NewReader(maxUint64.Bytes()), binary.BigEndian, &i)
c.Expect(err, IsNil)
c.Expect(i, Equals, uint64(math.MaxUint64))
})
c.Specify("generate a cryptography safe number in range [0, math.MaxUint64)", func() {
maxUint64 := NewMaxUint64()
for i := uint64(0); i < uint64(1e10); i++ {
num, err := rand.Int(rand.Reader, maxUint64)
c.Expect(err, IsNil)
// Check that num < maxUint64
c.Expect(num.Cmp(maxUint64), Equals, -1)
}
})
c.Specify("passwords are salted with 8 bytes", func() {
//i, err := rand.Int(rand.Reader, )
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment