go - 如何检查随机性?

标签 go encryption public-key-encryption

<分区>

我正在使用 BIP39 规范生成 12 个单词的助记符,稍后将用于生成主公钥/私钥及其 2^32-1 子 key 。然后,这些子 key 将用于非对称加密。

 import (
   "github.com/tyler-smith/go-bip39"
   "github.com/tyler-smith/go-bip32"
   b64 "encoding/base64"
 )
 type BipKeys struct {
     Entropy []byte
     Mnemonic string
     Passphrase []byte
     Seed []byte
     MnemonicShares []string
     RootPublicExtendedKey *bip32.Key
     RootPrivateExtendedKey *bip32.Key
     RootPrivateHexKey string
}

func(instance *BipKeys) GenerateEntropy(numberOfBytes int)([]byte, error){
   entropy, err := bip39.NewEntropy(numberOfBytes)
   if err != nil {
     log.Printf("There is some error generating entropy %s", err)
}
   return entropy, err
}

func (instance *BipKeys) GenerateMnemonic(entropy []byte) (string, error){
   mnemonic, err := bip39.NewMnemonic(entropy)
   if err != nil {
     log.Printf("Some error in generating Mnemonic %s", err)

 }
    return mnemonic, err
 }

func (instance *BipKeys) GeneratePassphrase(saltBytes int, passphraseBytes int) ([]byte, error){
  salt := GenerateRandomSalt(8)
  passphrase := GenerateRandomString(8)
  password, err := GenerateScryptKey(salt, []byte(passphrase))
  return password, err
}

GenerateRandomString 和 GenerateRandomSalt 只是根据 crypto/rand 包生成随机字符串和字节。

我的问题是,当人们说没有从随机函数正确生成的 key 容易被破解时,他们到底是什么意思? 我如何检查 bip39.NewEntropy(numberOfBytes) 是否实际生成了助记符所需的完美熵? 有什么方法可以检查仅通过公开前 5 个单词是否可以生成 12 个单词的助记符(这意味着熵函数实现不正确并且容易受到攻击)?

最佳答案

My question is, When people say that the keys not generated properly from random functions are susceptible to break, what they actually mean?

不良随机性在密码学中受到两种方式的攻击:

  1. 通过详尽搜索用于生成比特流的种子。一个突出的例子是 Debian OpenSSH vulnerability .

  2. 通过攻击并非为满足密码要求而设计的随机数生成器的可预测性。在统计随机数生成器中,主要要求是数据看起来是随机的。在密码随机数生成器中,我们需要更多:给定比特流,一个非常聪明的知道算法的人无法弄清楚随机数生成器的 future 或先前比特是什么。例如,尽管 Mersenne Twister内部状态较大,不满足密码学要求。

你关于测量熵的问题是错误的问题。测量熵将其视为统计随机数生成器,而不是加密随机数生成器。你说:

GenerateRandomString and GenerateRandomSalt just generates random string and bytes based on crypto/rand packages.

这就是您需要了解的全部内容——您无需担心其他任何事情。如上所述,再多的测量熵也无法告诉您您的 key 是安全的。相反,您需要成为密码学专家才能分析此类算法。对于随机性的消费者,您所能做的(除了使用其他来源,例如/dev/urandom 之外)就是相信这是经过精心设计并由密码专家分析的。

关于go - 如何检查随机性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52238575/

相关文章:

java - 如何用Java读取CSV文件然后通过AES算法加密

Java 到 Objective-C RSA 实现

go - 工作池实现以限制每秒的 http 请求

go - 堆栈跟踪中显示的 goroutine 编号是否在 golang 中重用或递增?

go - 包内有包声明 main 的两个 go 文件

java - 对称 key 加密算法

go - beego中可以使用多个注解吗?

php - 有没有一种方法可以在没有 PHP 随机数的情况下使用 Sodium Encrypt?

azure - 如何安全使用Azure Key Vault?

go - 无法在golang中使用公钥加密