Golang如何構(gòu)造最佳隨機(jī)密碼詳解
為了保護(hù)系統(tǒng)或數(shù)據(jù)安全,我們需要最佳隨機(jī)密碼。這里使用unix系統(tǒng)定義的文件設(shè)備/dev/random
,從中獲取隨機(jī)數(shù)生成器的種子。
需求說明
定義程序goodPass.go,程序需要一個(gè)可選命令行參數(shù),指定生成密碼的長(zhǎng)度,缺省長(zhǎng)度為10. 另外生成密碼的ASCII從!
到z
,對(duì)應(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ù)值。下面實(shí)現(xiàn)main函數(shù),處理命令行參數(shù),并從隨機(jī)文件設(shè)備中獲取隨機(jī)種子:
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ù),如果沒有指定長(zhǎng)度,則取默認(rèn)值10,否則解析命令行參數(shù)。
然后打開/dev/random
設(shè)備進(jìn)行讀取,這里使用binary.Read
是需要指定字節(jié)順序(binary.LittleEndian),這是為了構(gòu)建int64類型,而不是獲得一串字節(jié)。這里為了展示如何從二進(jìn)制文件讀內(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字符有點(diǎn)奇怪,這是因?yàn)镚o默認(rèn)支持Unicode字符。因此需要轉(zhuǎn)換整數(shù)值ascii字符,對(duì)應(yīng)代碼為:
newChar := string(startChar[0] + byte(anInt))
運(yùn)行程序,生成下列輸出:
$ 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)造最佳隨機(jī)密碼的文章就介紹到這了,更多相關(guān)Golang最佳隨機(jī)密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang實(shí)現(xiàn)不被復(fù)制的結(jié)構(gòu)體的方法
sync包中的許多結(jié)構(gòu)都是不允許拷貝的,因?yàn)樗鼈冏陨泶鎯?chǔ)了一些狀態(tài)(比如等待者的數(shù)量),如果你嘗試復(fù)制這些結(jié)構(gòu)體,就會(huì)在你的?IDE中看到警告,那這是怎么實(shí)現(xiàn)的呢,下文就來和大家詳細(xì)講講2023-03-03從淺入深帶你掌握Golang數(shù)據(jù)結(jié)構(gòu)map
在?Go?語言中,map?是一種非常常見的數(shù)據(jù)類型,它可以用于快速地檢索數(shù)據(jù)。本篇文章將介紹?Go?語言中的?map,包括?map?的定義、初始化、操作和優(yōu)化,需要的可以參考一下2023-04-04淺析go中的map數(shù)據(jù)結(jié)構(gòu)字典
golang中的map是一種數(shù)據(jù)類型,將鍵與值綁定到一起,底層是用哈希表實(shí)現(xiàn)的,可以快速的通過鍵找到對(duì)應(yīng)的值。這篇文章主要介紹了go中的數(shù)據(jù)結(jié)構(gòu)字典-map,需要的朋友可以參考下2019-11-11