Source file src/crypto/rand/rand_wasip1.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build wasip1
     6  
     7  package rand
     8  
     9  import "syscall"
    10  
    11  func init() {
    12  	Reader = &reader{}
    13  }
    14  
    15  type reader struct{}
    16  
    17  func (r *reader) Read(b []byte) (int, error) {
    18  	// This uses the wasi_snapshot_preview1 random_get syscall defined in
    19  	// https://github.com/WebAssembly/WASI/blob/23a52736049f4327dd335434851d5dc40ab7cad1/legacy/preview1/docs.md#-random_getbuf-pointeru8-buf_len-size---result-errno.
    20  	// The definition does not explicitly guarantee that the entire buffer will
    21  	// be filled, but this appears to be the case in all runtimes tested.
    22  	err := syscall.RandomGet(b)
    23  	if err != nil {
    24  		return 0, err
    25  	}
    26  	return len(b), nil
    27  }
    28  

View as plain text