0% found this document useful (0 votes)
63 views6 pages

Cryptographically Secure Random Number Generator

The document describes the implementation of a cryptographically secure random number generator in the Go programming language. It details the various operating system APIs used for generating random numbers across different platforms and includes a global instance called 'Reader' that is safe for concurrent use. Additionally, it outlines the behavior of the 'Read' function, which fills a byte slice with random data and handles errors by crashing the program if they occur.

Uploaded by

sridgeinfo353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

Cryptographically Secure Random Number Generator

The document describes the implementation of a cryptographically secure random number generator in the Go programming language. It details the various operating system APIs used for generating random numbers across different platforms and includes a global instance called 'Reader' that is safe for concurrent use. Additionally, it outlines the behavior of the 'Read' function, which fills a byte slice with random data and handles errors by crashing the program if they occur.

Uploaded by

sridgeinfo353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59
60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

Code panel - press c to focus line 1.

// Copyright 2010 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Package rand implements a cryptographically secure


// random number generator.

package rand

import (

"crypto/internal/boring"

"crypto/internal/fips140"

"crypto/internal/fips140/drbg"

"crypto/internal/sysrand"

"io"

_ "unsafe"

// Reader is a global, shared instance of a cryptographically

// secure random number generator. It is safe for concurrent use.

//

// - On Linux, FreeBSD, Dragonfly, and Solaris, Reader uses getrandom(2).

// - On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.

// - On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).

// - On NetBSD, Reader uses the [Link] sysctl.

// - On Windows, Reader uses the ProcessPrng API.

// - On js/wasm, Reader uses the Web Crypto API.

// - On wasip1/wasm, Reader uses random_get.

//

// In FIPS 140-3 mode, the output passes through an SP 800-90A Rev. 1

// Deterministric Random Bit Generator (DRBG).

var Reader [Link]

func init() {

if [Link] {

Reader = [Link]

return

Reader = &reader{}
}

type reader struct {

[Link]

func (r *reader) Read(b []byte) (n int, err error) {

[Link]()

if [Link] {

[Link](b)

} else {

[Link](b)

return len(b), nil

// fatal is [[Link]], pushed via linkname.

//

//go:linkname fatal

func fatal(string)

// Read fills b with cryptographically secure random bytes. It never returns an

// error, and always fills b entirely.

//

// Read calls [[Link]] on [Reader] and crashes the program irrecoverably if

// an error is returned. The default Reader uses operating system APIs that are

// documented to never return an error on all but legacy Linux systems.

func Read(b []byte) (n int, err error) {

// We don't want b to escape to the heap, but escape analysis can't see

// through a potentially overridden Reader, so we special-case the default

// case which we can keep non-escaping, and in the general case we read into

// a heap buffer and copy from it.

if r, ok := Reader.(*reader); ok {
_, err = [Link](b)

} else {

bb := make([]byte, len(b))

_, err = [Link](Reader, bb)

copy(b, bb)

if err != nil {

fatal("crypto/rand: failed to read random data (see


[Link] " + [Link]())

panic("unreachable") // To be sure.

return len(b), nil

History

References

keyboard_capslock

We serve cookies on this site to analyze traffic, remember your preferences, and
optimize your experience.

You might also like