Ejemplo n.º 1
0
func main() {
	rng, _ := fortuna.NewRNG("")

	out, err := os.Create(outputFileName)
	if err != nil {
		log.Fatalf("cannot open %s: %s", outputFileName, err.Error())
	}
	defer out.Close()

	n, _ := io.CopyN(out, rng, outputFileSize)
	log.Printf("wrote %d random bytes to %s", n, outputFileName)
}
Ejemplo n.º 2
0
func main() {
	trace.Register(printTrace, "", trace.PrioDebug)

	rng, err := fortuna.NewRNG(seedFileName)
	if err != nil {
		panic("cannot initialise the RNG: " + err.Error())
	}
	defer rng.Close()

	// entropy source 1: submit some randomness from crypto/rand once a minute
	go func() {
		sink1 := rng.NewEntropyDataSink()
		for _ = range time.Tick(time.Minute) {
			buffer := make([]byte, 4)
			n, _ := rand.Read(buffer)
			sink1 <- buffer[:n]
		}
	}()

	// entropy source 2: submit time between requests
	sink2 := rng.NewEntropyTimeStampSink()
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		sink2 <- time.Now()

		sizeStr := r.URL.Query().Get("len")
		size, _ := strconv.ParseInt(sizeStr, 0, 32)
		if size <= 0 {
			size = 16
		}
		w.Header().Set("Content-Length", fmt.Sprintf("%d", size))

		io.CopyN(w, rng, size)
		trace.T("main", trace.PrioInfo,
			"sent %d random bytes for %q", size, r.RequestURI)
	})

	listenAddr := ":8080"
	trace.T("main", trace.PrioInfo,
		"listening on http://localhost%s/", listenAddr)
	err = http.ListenAndServe(listenAddr, nil)
	if err != nil {
		trace.T("main", trace.PrioCritical, "%s", err.Error())
	}
}