- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Go 有两个随机数包:
crypto/rand
,它提供了一种获取随机字节的方法math/rand
,它有一个很好的洗牌算法我想使用 math/rand
中的 Perm
算法,但要为其提供高质量的随机数。
因为两个 rand
包是同一个标准库的一部分,所以应该有一种方法将它们组合在一起,以便 crypto/rand
提供一个很好的源math/rand.Perm
用来生成排列的随机数。
这里(以及 Playground )是我为连接这两个包而编写的代码:
package main
import (
cryptoRand "crypto/rand"
"encoding/binary"
"fmt"
mathRand "math/rand"
)
type cryptoSource struct{}
func (s cryptoSource) Int63() int64 {
bytes := make([]byte, 8, 8)
cryptoRand.Read(bytes)
return int64(binary.BigEndian.Uint64(bytes) >> 1)
}
func (s cryptoSource) Seed(seed int64) {
panic("seed")
}
func main() {
rnd := mathRand.New(&cryptoSource{})
perm := rnd.Perm(52)
fmt.Println(perm)
}
此代码有效。理想情况下,我不想自己定义 cryptoSource
类型,而只是将两个 rand
包放在一起,以便它们一起工作。那么在某处是否有此 cryptoSource
类型的预定义版本?
最佳答案
这基本上就是您需要做的。对于 math/rand
的常见用法,您通常不需要加密安全的随机源,因此没有提供适配器。通过直接在值中分配缓冲区空间,而不是在每次调用时都分配一个新 slice ,您可以使实现稍微高效一些。然而,在读取操作系统随机源失败的不太可能发生的情况下,这将需要 panic 以防止返回无效结果。
type cryptoSource [8]byte
func (s *cryptoSource) Int63() int64 {
_, err := cryptoRand.Read(s[:])
if err != nil {
panic(err)
}
return int64(binary.BigEndian.Uint64(s[:]) & (1<<63 - 1))
}
关于random - 使用 crypto/rand 通过 rand.Perm 生成排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965044/
我正在尝试更新一些较旧的代码以消除警告。我已经升级了一些使用加密模块的代码。然而,webpack 5.88.2没有识别新的语法。我的新导入内容如下所示:。完整的错误如下所示。这表明我可能需要另一个插件
是否有任何 C 或 C++ 函数可以产生与此 python 代码相同的输出(SHA512 with salt)? import crypt; crypt.crypt('test', '$6$Salte
我正在开发一个使用 Crypto++ 使用 RSA 加密一些数据的项目。 这是我的 Crypto++ 代码: string plain = "Text123", encoded, cipher; st
收到错误,因为 require 不是一个函数,尝试在 typescript 组件中使用加密 js 函数 // Load modules 'use strict'; var Crypto = requi
我正在尝试将下面的java代码转换为nodejs。 public static String encrypt(String accessToken) throws Exception {
我有一个旨在在 Node.js 中使用的签名方法,但我想使用 crypto-js 在客户端实现它。它应该可以在最新的 Chrome 版本中运行。 我尝试遵循以下一些答案:Decode a Base64
我想知道 Crypto.Signature.PKCS1_v1_5 和 Crypto.Signature.pkcs1_15 有什么区别? 在documentation他们使用此函数 Crypto.Sig
我们有一个使用 Crypto++ 库的 ECC 部分的 C++ 解决方案,但必须转移到 .NET 解决方案。由于 Microsoft 的 ECC 代码的文档最少,我目前正在试验文档最少的 Bouncy
我在验证 Web Crypto API 创建的签名时遇到问题。 这是我用来在浏览器中生成 RSA key 的代码: let keys; const generateKeys = async () =>
嗨,所以我有一个运行 Crypto 的服务器,它工作得很好。我使用 electrojs 作为客户端,并且加密应该内置到 Node 中。当我尝试使用该模块时,它返回“crypto.scryptSync
使用 Apple 的 Common Crypto 和 Crypto++ 使用相同的 key 加密相同的文件(二进制数据)时,我得到了不同的结果。我使用的算法是 AES。 这是使用 Common Cry
如何存储 crypto.createHash('sha1') 的当前状态(在它被 hash.update(buffer) 填充后)以在另一个地方使用它http请求可能发生在node.js的不同进程?
我正在使用 NodeJS 的捆绑 crypto服务器端的 SHA256 哈希模块。在客户端,我使用一个名为 Crypto-JS 的 javascript 库. 我将 SHA256 哈希值用于使用基于经
我想编写一个使用 linux crypto-api 进行数字签名的 C 程序。不幸的是,我找不到关于 linux api 和 linux/crypto.h 中定义的函数的良好文档(谷歌搜索没有帮助,这
我正在尝试使用 JUNIT 在实际执行 AES 加密的方法中模拟 cipher.doFinal()。我在启动 JUNIT 测试用例时遇到异常 "Tried to access class javax.
您好,我在Liferay dxp项目中使用Paytm校验和依赖项 但我得到error :com.sun.crypto.provider.AESCipher$General cannot be cast
我正在尝试使用 crypto-js javascript 库加密数据,并尝试使用 Node 加密库在 Nodejs 端解密相同的加密文本。我正在使用 AES 256 加密算法和 CTR 模式,没有填充
我想了解 javax.crypto.Mac 之间的区别和 javax.crypto.Cipher 。这两个类看起来非常相似(它们具有相似的方法,但是这两个类并不互相继承)。 这两个类之间的根本区别是什
我正在尝试在设备上生成 SHA256 和 HmacSHA512 哈希值,不幸的是,该设备不支持标准 Node crypto 库。所以我正在调整代码以使用 CryptoJS 代替。但是,CryptoJS
我正在使用 Android FingerPrintManager API 并使用 KeyPairGenerator 创建 key 对,我想使用公钥加密密码,然后在用户通过输入 fingerPrint
我是一名优秀的程序员,十分优秀!