Golang Gorm Fiber / argon2.Config 未定义

问题描述 投票:0回答:1

我正在尝试从 PHP 切换到 GO,但我陷入了困境,我请求你的帮助。

我正在尝试使用 Argon2 创建密码哈希函数,但当我使用“argon2.Config{}”时,我不断收到错误“未定义:argon2.Config”。我已经多次重新编译该项目,但似乎找不到解决方案。我请求您帮助解决这个问题。

func hashPassword(password string) []byte {
    // Şifreleme parametreleri
    timeCost := 1                 // İşlem süresi
    memory := 64 * 1024           // // Bellek miktarı
    threads := 4                  //  İş parçacığı sayısı
    keyLength := 32               // Oluşturulacak hash uzunluğu
    salt := []byte("unique_salt") // Her kullanıcı için benzersiz

    // Argon2 işlemi için hasher oluştur
    hasher := argon2.Config{
        Time:    uint32(timeCost),
        Memory:  uint32(memory),
        Threads: uint8(threads),
        KeyLen:  uint32(keyLength),
    }

    // Şifreyi hashle
    hashedPassword := hasher.Hash(password, salt, nil)

    return hashedPassword
}

go go-gorm go-templates go-fiber
1个回答
0
投票

如果您使用的是包

"golang.org/x/crypto/argon2"
,您可以使用
argon2.IDKey()
方法。这是一个工作示例:

func HashPassword(password string) (hashedPassword string) {

    const (
        timeCost  uint32 = 1         // İşlem süresi
        memory    uint32 = 64 * 1024 // // Bellek miktarı
        threads   uint8  = 4         //  İş parçacığı sayısı
        keyLength uint32 = 32        // Oluşturulacak hash uzunluğu
    )

    salt := []byte("unique_salt") // Her kullanıcı için benzersiz

    // generate hashedpassword
    hash := argon2.IDKey([]byte(password), salt, timeCost, memory, threads, keyLength)

    // Base64 encode the salt and hashed password.
    b64Salt := base64.RawStdEncoding.EncodeToString(salt)
    b64Hash := base64.RawStdEncoding.EncodeToString(hash)

    format := "$argon2id$v=%d$models=%d,t=%d,p=%d$%s$%s"

    // final password in recommended format
    hashedPassword = fmt.Sprintf(format, argon2.Version, memory, timeCost, threads, b64Salt, b64Hash)
    return hashedPassword
}
© www.soinside.com 2019 - 2024. All rights reserved.