首頁 > 後端開發 > Golang > 主體

用 Go 建構密碼管理器

DDD
發布: 2024-09-18 20:17:30
原創
839 人瀏覽過

身為軟體開發人員,我一直對安全性和可用性的交集著迷。最近,我決定開始一個令人興奮的專案:使用 Go 建立一個命令列密碼管理器。我想與您分享這段旅程的開始,從第一次提交開始。

創世記

2023 年 11 月 27 日,我對我的專案進行了初步提交,我將其命名為「dost」(印地語中的朋友,反映了它作為密碼管理的有用伴侶的角色)。這第一步雖然很小,但卻為我希望成為一個強大且用戶友好的工具奠定了基礎。

靈感與願景

Building a Password Manager in Go

在開始這個專案時,我從流行的命令列密碼管理器 pass 中汲取了靈感。 pass 的簡單性和有效性引起了我的注意,我決定使用它的 API 作為在 Go 中建立我自己的密碼管理器的藍圖。

深入研究 pass 的原始碼是令人大開眼界的經驗。我很有興趣地發現這項廣泛使用的工具的全部功能都封裝在一個綜合的 Bash 腳本中。這種優雅的簡單性是我所欣賞的,並希望在我自己的專案中效仿,儘管使用了 Go 的優勢。

透過學習 pass,我對命令列密碼管理器的基本功能及其應提供的使用者體驗獲得了寶貴的見解。當我繼續開發「dost」時,我將牢記這些教訓,旨在創建一個將 pass 的簡單性與 Go 的效能和跨平台相容性優勢結合起來的工具。

這次探索不僅為要實現的功能提供了路線圖,而且增強了我對精心設計、專注的工具的力量的信念。我很高興看到這種靈感將如何影響「dost」在未來發展階段的演變。

第一個特點

最初的提交重點在於兩個核心功能:

  1. 密碼產生:我實作了一個基本的密碼產生器,讓使用者可以指定他們所需的密碼長度。此功能旨在創建適合各種安全要求的強隨機密碼。

  2. 剪貼簿整合:為了增強使用者體驗,我確保產生的密碼會自動複製到剪貼簿。這個雖小但至關重要的功能可以節省時間並降低轉錄錯誤的風險。

技術見解

讓我們深入探討第一次迭代的一些技術面:

  • Go 版本:該專案使用 Go 1.21.0 構建,充分利用了該語言的簡單性和高效性。
  • 外部相依性:我正在使用 github.com/atotto/clipboard 套件來無縫處理不同作業系統之間的剪貼簿操作。
  • 隨機生成:密碼生成利用 Go 的 crypto/rand 套件進行安全隨機數生成,這對於創建不可預測的強密碼至關重要。
  • 字符集:密碼產生器包括大小寫字母、數字和各種特殊字符,以確保複雜性。

程式碼片段

讓我們來看看實現的一些關鍵部分:

  1. 密碼產生功能:
func generatePassword(length int) (string, error) {
    const (
        uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
        digits           = "0123456789"
        specialChars     = "!@#$%^&*()-_=+[]{}|;:'\",.<>/?"
    )

    allChars := uppercaseLetters + lowercaseLetters + digits + specialChars

    var password string
    for i := 0; i < length; i++ {
        randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(allChars))))
        if err != nil {
            return "", err
        }
        password += string(allChars[randomIndex.Int64()])
    }

    return password, nil
}
登入後複製

此函數透過從預定義集中隨機選擇字元來建立密碼,確保大寫、小寫、數字和特殊字元的混合。

  1. 剪貼簿整合:
func writeToClipboard(text string) error {
    return clipboard.WriteAll(text)
}
登入後複製

這個簡單的函數利用剪貼簿包將產生的密碼寫入系統剪貼簿。

  1. 主要功能:
func main() {
    passwordLength := flag.Int("length", 12, "length of your password")
    flag.Parse()

    password, err1 := generatePassword(*passwordLength)
    if err1 != nil {
        fmt.Println("Error generating password:", err1)
        return
    }

    fmt.Println("Generated Password:", password)

    err2 := writeToClipboard(password)
    if err2 != nil {
        fmt.Println("Error writing to clipboard:", err2)
        os.Exit(1)
    }

    fmt.Println("Copied to clipboard! ✅\n")
}
登入後複製

主要功能將所有內容連結在一起。它使用Go的flag包來允許使用者指定密碼長度,產生密碼,並將其複製到剪貼簿。

命令列介面

如你在 main 函數中看到的,我使用 Go 的 flag 套件實作了一個簡單的 CLI。使用者可以使用 -length 標誌指定所需的密碼長度,如果未指定,則預設為 12 個字元。

展望未來

第一次提交只是一個開始。當我繼續開發這個密碼管理器時,我計劃添加以下功能:

  • Secure storage of passwords
  • Encryption of stored data
  • Search and retrieval functionalities
  • Password strength analysis

I'm excited about the journey ahead and the challenges it will bring. Building a password manager is not just about coding; it's about understanding security principles, user needs, and creating a tool that people can trust with their sensitive information.

Stay tuned for more updates as this project evolves. I'll be sharing my progress, challenges, and learnings along the way. If you're interested in following along or contributing, feel free to check out the project on GitHub.

Building a Password Manager in Go svemaraju / dost

dost command line password manager written in Go

dost

dost is a CLI password manager written in Go.

Inspired by (Pass)[https://www.passwordstore.org/]

Features

  • Generate random passwords of configurable length
  • Copy generated passwords to clipboard automatically
  • Skip using symbols

Usage

> go build -o dost main.go
登入後複製
Enter fullscreen mode Exit fullscreen mode

Generating password:

> ./dost generate email/vema@example.com
Generated Password: );XE,7-Dv?)Aa+&<{V-|pKuq5
登入後複製

Generating password with specified length (default is 25):

> ./dost generate email/vema@example.com 12
Generated Password: si<yJ=5/lEb3
登入後複製

Copy generated password to clipboard without printing:

> ./dost generate -c email/vema@example.com 
Copied to clipboard! ✅
登入後複製

Avoid symbols for generating passwords:

> ./dost generate -n email/vema@example.com 
Generated Password: E2UST}^{Ac[Fb&D|cD%;Eij>H
登入後複製

Under development

  • Insert a new password manually
  • Show an existing password
  • List all entries
  • Password storage
  • GPG Key based encryption

License

MIT




View on GitHub


以上是用 Go 建構密碼管理器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!