網(wǎng)站首頁 編程語言 正文
為了保護系統(tǒng)或數(shù)據(jù)安全,我們需要最佳隨機密碼。這里使用unix系統(tǒng)定義的文件設備/dev/random
,從中獲取隨機數(shù)生成器的種子。
需求說明
定義程序goodPass.go,程序需要一個可選命令行參數(shù),指定生成密碼的長度,缺省長度為10. 另外生成密碼的ASCII從!
到z
,對應ascii碼為33到122。
程序第一部分是導入相應的包:
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()結果不包括末端數(shù)值。下面實現(xiàn)main函數(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ù),如果沒有指定長度,則取默認值10,否則解析命令行參數(shù)。
然后打開/dev/random
設備進行讀取,這里使用binary.Read
是需要指定字節(jié)順序(binary.LittleEndian),這是為了構建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默認支持Unicode字符。因此需要轉換整數(shù)值ascii字符,對應代碼為:
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
總結
原文鏈接:https://blog.csdn.net/neweastsun/article/details/128493527
相關推薦
- 2021-12-29 Android中String與int相互轉換_Android
- 2022-04-20 Django學習之靜態(tài)文件與模板詳解_python
- 2022-03-27 Android實現(xiàn)房貸計算器功能_Android
- 2022-03-08 用C語言實現(xiàn)鏈式棧介紹_C 語言
- 2022-09-19 golang圖片處理庫image基本操作_Golang
- 2022-04-23 R語言繪制line?plot線圖示例詳解_R語言
- 2022-07-30 jQuery?UI組件介紹_jquery
- 2022-09-13 Linux中一對多配置日志服務器的詳細步驟_Linux
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支