Golang如何構(gòu)造最佳隨機密碼詳解
為了保護系統(tǒng)或數(shù)據(jù)安全,我們需要最佳隨機密碼。這里使用unix系統(tǒng)定義的文件設(shè)備/dev/random,從中獲取隨機數(shù)生成器的種子。
需求說明
定義程序goodPass.go,程序需要一個可選命令行參數(shù),指定生成密碼的長度,缺省長度為10. 另外生成密碼的ASCII從!到z,對應(yīng)ascii碼為33到122。
程序第一部分是導(dǎo)入相應(yīng)的包:
package main
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
"path/filepath"
"strconv"
)
var MAX = 90
var MIN = 0
// Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n)
// from the default Source.
// It panics if n <= 0.
func random(min, max int) int {
return rand.Intn(max-min) + min
}
這里radmon函數(shù)生成一定范圍內(nèi)的,Intn()結(jié)果不包括末端數(shù)值。下面實現(xiàn)main函數(shù),處理命令行參數(shù),并從隨機文件設(shè)備中獲取隨機種子:
func main() {
var LENGTH int64 = 10
if len(os.Args) != 2 {
fmt.Printf("usage: %s length\n", filepath.Base(os.Args[0]))
//os.Exit(1)
fmt.Printf("Default length is %d\n", LENGTH)
} else {
LENGTH, _ = strconv.ParseInt(os.Args[1], 10, 64)
}
f, _ := os.Open("/dev/random")
var seed int64
_ = binary.Read(f, binary.LittleEndian, &seed)
_ = f.Close()
rand.Seed(seed)
fmt.Println("Seed:", seed)
GenPass(LENGTH)
}
首先處理命令行參數(shù),如果沒有指定長度,則取默認(rèn)值10,否則解析命令行參數(shù)。
然后打開/dev/random 設(shè)備進行讀取,這里使用binary.Read是需要指定字節(jié)順序(binary.LittleEndian),這是為了構(gòu)建int64類型,而不是獲得一串字節(jié)。這里為了展示如何從二進制文件讀內(nèi)容至Go類型。
binary.Read(f, binary.LittleEndian, &seed) 函數(shù)的源碼注釋為:
// Read reads structured binary data from r into data. // Data must be a pointer to a fixed-size value or a slice of fixed-size values. // Bytes read from r are decoded using the specified byte order and written to successive fields of the data. // When decoding boolean values, a zero byte is decoded as false, and any other non-zero byte is decoded as true.
最后一部分代碼為:
func GenPass(LENGTH int64) {
startChar := "!"
var i int64
for i = 0; i < LENGTH; i++ {
anInt := random(MIN, MAX)
newChar := string(startChar[0] + byte(anInt))
if newChar == " " {
i = i - i
continue
}
fmt.Print(newChar)
}
fmt.Println()
}我們看到Go處理Ascii字符有點奇怪,這是因為Go默認(rèn)支持Unicode字符。因此需要轉(zhuǎn)換整數(shù)值ascii字符,對應(yīng)代碼為:
newChar := string(startChar[0] + byte(anInt))
運行程序,生成下列輸出:
$ go run goodPass.go 1 Seed: -5195038511418503382 b $ go run goodPass.go 10 Seed: 8492864627151568776 k43Ve`+YD) $ go run goodPass.go 50 Seed: -4276736612056007162 !=Gy+;XV>6eviuR=ST\u:Mk4Q875Y4YZiZhq&q_4Ih/]''`2:x
總結(jié)
到此這篇關(guān)于Golang如何構(gòu)造最佳隨機密碼的文章就介紹到這了,更多相關(guān)Golang最佳隨機密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Go?Json?Unmarshal反序列化丟失數(shù)字精度問題
業(yè)務(wù)會使用?id生成器?產(chǎn)生的?分布式唯一ID,長度比較長,所以代碼反序列化時,會出現(xiàn)精度丟失問題,那如何解決呢,下面小編就來和大家詳細(xì)講講2023-08-08
Go語言中實現(xiàn)Unix風(fēng)格的進程管道方法實例
這篇文章主要為大家介紹了Go語言中實現(xiàn)Unix風(fēng)格的進程管道方法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Golang實現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的示例詳解
這篇文章主要為大家詳細(xì)介紹了Golang實現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的方法,文中的示例代碼講解詳細(xì),對學(xué)習(xí)Go語言有一定的幫助,需要的可以參考一下2023-02-02
Go結(jié)合Redis用最簡單的方式實現(xiàn)分布式鎖
本文主要介紹了Go結(jié)合Redis用最簡單的方式實現(xiàn)分布式鎖示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

