rand.Read() 和 io.ReadFull(rand.Reader) 的区别?
golang的随机包 rand.go 中我们可以看到 rand.Read 其实是调用的io.Reader.Read()
1: // Package rand implements a cryptographically secure
2: // pseudorandom number generator.
3: package rand
4:
5: import "io"
6:
7: // Reader is a global, shared instance of a cryptographically
8: // strong pseudo-random generator.
9: // On Unix-like systems, Reader reads from /dev/urandom.
10: // On Windows systems, Reader uses the CryptGenRandom API.
11: var Reader io.Reader
12:
13: // Read is a helper function that calls Reader.Read.
14: func Read(b []byte) (n int, err error) { return Reader.Read(b) }
io.Reader.Read() 函数的说明如下:
Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
简单来说,它读出的数据,并不一定是指定长度的。
io.ReadFull 函数则相反:
ReadFull reads exactly len(buf) bytes from r into buf. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF. On return, n == len(buf) if and only if err == nil.
读取正好len(buf)长度的字节。如果字节数不是指定长度,则返回错误信息和正确的字节数。
当没有字节能被读时,返回EOF错误。
如果读了一些,但是没读完产生EOF错误时,返回ErrUnexpectedEOF错误。
综上对比,获取随机数时,最好是下面的写法。
1: import crand "crypto/rand"
2:
3: r := make([]byte, length)
4:
5: if _, err := io.ReadFull(crand.Reader, r); err != nil {
6: panic("error reading from random source: " + err.Error())
7: }
参考资料:
rand.Read() or io.ReadFull(rand.Reader)?
https://groups.google.com/forum/#!topic/golang-nuts/eFS5GN-en3w
rand.Read() 和 io.ReadFull(rand.Reader) 的区别?的更多相关文章
- golang io.ReadFull
buf := make([]byte, 10, 10) file, _ := os.Open("./data.txt") n, err := io.ReadFull(file, b ...
- PHP随机函数rand()、mt_rand()、srand()、mt_srand() 的区别
1.生成随机数发生器种子的函数 srand(). mt_srand() 区别:mt_srand() 比 srand() 更好的生成随机数发生器种子 定义: void srand([int $seed ...
- 013-java中的IO操作-InputStream/Reader、OutputStream/Writer
一.概述 IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称 ...
- Linux IO函数的使用和区别
Linux系统中的IO函数主要有read.write.recv.send.recvmsg.sendmsg.readv.writev,本篇主要介绍他们的使用以及区别. read函数: #include ...
- 简述同步IO、异步IO、阻塞IO、非阻塞IO之间的联系与区别
POSIX 同步IO.异步IO.阻塞IO.非阻塞IO,这几个词常见于各种各样的与网络相关的文章之中,往往不同上下文中它们的意思是不一样的,以致于我在很长一段时间对此感到困惑,所以想写一篇文章整理一下. ...
- 同步IO、异步IO、阻塞IO、非阻塞IO之间的联系与区别
POSIX 同步IO.异步IO.阻塞IO.非阻塞IO,这几个词常见于各种各样的与网络相关的文章之中,往往不同上下文中它们的意思是不一样的,以致于我在很长一段时间对此感到困惑,所以想写一篇文章整理一下. ...
- Java中输入时IO包与Scanner的区别
最常用的一个IO控制台输入的 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream ...
- stm32 IO口八种模式区别
初学STM32,遇到I/O口八种模式的介绍,网上查了一下资料,下面简明写出这几种模式的区别,有不对的地方请大家多多指正! 上拉输入模式:区别在于没有输入信号的时候默认输入高电平(因为有弱上拉).下拉输 ...
- go标准库的学习-crypto/rand
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/rand" rand包实现了用于加解密的更安全的随机数生成器. Var ...
随机推荐
- http://blog.csdn.net/xiamizy/article/details/40781939
http://blog.csdn.net/xiamizy/article/details/40781939
- [转载]TexturePacker 如何使用自带的加密功能及在cocos2dx中的使用
在cocos2dx中使用纹理图集是非常节省资源的,在这里推荐 TexturePacker,而且 TexturePacker工具的加密接口也非常的好用,下面就来介绍一下... TexturePacker ...
- C++调用python
本文以实例code讲解 C++ 调用 python 的方法. 本文在util.h中实现三个函数: 1. init_log: 用google log(glog)初始化log 2. exe_command ...
- ccnu-线段树-简单的区间更新(三题)
题目一:http://poj.org/problem?id=3468 Description You have N integers, A1, A2, ... , AN. You need to de ...
- 6.cadence原理图下[原创]
一.平坦式原理图与分页式原理图 1 和2为平坦式原理图,平等 1和3为分页式原理图 有上下关系 ------------------------------------ 1.平坦式原理图 每张原理 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- [POJ2777]Count Color(线段树)
题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...
- leetcode:Sort List(一个链表的归并排序)
Sort a linked list in O(n log n) time using constant space complexity. 分析:题目要求时间复杂度为O(nlogn),所以不能用qu ...
- JAVA的序列化与反序列化
一.为什么要进行序列化 再介绍之前,我们有必要先了解下对象的生命周期,我们知道Java对象的生命周期,也即Java中的远程方法调用RMI也会被用到,在网络中要传输对象的话,则必须要对对象进行序列化,关 ...
- android SDK 更新
在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...