doulu7258 2010-04-18 14:45
浏览 36
已采纳

Go-有关加密/兰特的示例

Could put a little example about the use of crypto/rand [1]?

The function Read has as parameter an array of bytes. Why? If it access to /dev/urandom to get the random data.

func Read(b []byte) (n int, err os.Error)

[1] http://golang.org/pkg/crypto/rand/

  • 写回答

1条回答 默认 最新

  • dsxon40042 2010-04-18 17:05
    关注
    func Read(b []byte) (n int, err os.Error)
    

    Read is a helper function that calls Reader.Read. Reader is defined as: var Reader io.Reader.

    crypto/rand/

    io.Reader is the interface that wraps the basic Read method.

    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 rather than block waiting for more.

    At the end of the input stream, Read returns 0, os.EOF. Read may return a non-zero number of bytes with a non-nil err. In particular, a Read that exhausts the input may return n > 0, os.EOF.

    type Reader interface {
        Read(p []byte) (n int, err os.Error)
    }
    

    io/#Reader

    For example, to read the first 16 random bytes,

    package main
    
    import (
        "fmt"
        "crypto/rand"
    )
    
    func main() {
        b := make([]byte, 16)
        n, err := rand.Read(b)
        fmt.Println(n, err, b)
    }
    

    Using a package init() function, crypto/rand defaults to using /dev/urandom.

    // Easy implementation: read from /dev/urandom.
    // This is sufficient on Linux, OS X, and FreeBSD.
    func init() { Reader = &devReader{name: "/dev/urandom"} }
    

    crypto/rand/rand.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路