github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/internal/rng/rng.go (about)

     1  // https://github.com/usbarmory/tamago
     2  //
     3  // Copyright (c) WithSecure Corporation
     4  // https://foundry.withsecure.com
     5  //
     6  // Use of this source code is governed by the license
     7  // that can be found in the LICENSE file.
     8  
     9  package rng
    10  
    11  import (
    12  	_ "unsafe"
    13  )
    14  
    15  var GetRandomDataFn func([]byte)
    16  
    17  //go:linkname getRandomData runtime.getRandomData
    18  func getRandomData(b []byte) {
    19  	GetRandomDataFn(b)
    20  }
    21  
    22  func Fill(b []byte, index int, val uint32) int {
    23  	shift := 0
    24  	limit := len(b)
    25  
    26  	for (index < limit) && (shift <= 24) {
    27  		b[index] = byte((val >> shift) & 0xff)
    28  		index += 1
    29  		shift += 8
    30  	}
    31  
    32  	return index
    33  }