網站首頁 編程語言 正文
為了保護系統或數據安全,我們需要最佳隨機密碼。這里使用unix系統定義的文件設備/dev/random
,從中獲取隨機數生成器的種子。
需求說明
定義程序goodPass.go,程序需要一個可選命令行參數,指定生成密碼的長度,缺省長度為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函數生成一定范圍內的,Intn()結果不包括末端數值。下面實現main函數,處理命令行參數,并從隨機文件設備中獲取隨機種子:
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) }
首先處理命令行參數,如果沒有指定長度,則取默認值10,否則解析命令行參數。
然后打開/dev/random
設備進行讀取,這里使用binary.Read
是需要指定字節順序(binary.LittleEndian),這是為了構建int64類型,而不是獲得一串字節。這里為了展示如何從二進制文件讀內容至Go類型。
binary.Read(f, binary.LittleEndian, &seed) 函數的源碼注釋為:
// 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字符。因此需要轉換整數值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
相關推薦
- 2022-08-02 Shell判斷字符串變量是否為空的方法實現_linux shell
- 2022-11-28 golang進程在docker中OOM后hang住問題解析_Golang
- 2022-11-16 詳解C++中的左值,純右值和將亡值_C 語言
- 2022-06-10 Linux環境下部署Consul集群_Linux
- 2022-12-07 C++游戲教程基本技巧之隨機化詳解_C 語言
- 2022-08-18 R語言使用cgdsr包獲取TCGA數據示例詳解_R語言
- 2022-11-25 使用PyTorch常見4個錯誤解決示例詳解_python
- 2022-10-05 Iptables防火墻四表五鏈概念及使用技巧詳解_安全相關
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支