登录
首页 >  Golang >  Go问答

未定义:Golang中使用Gorm Fiber / argon2.Config

来源:stackoverflow

时间:2024-02-03 17:57:19 277浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《未定义:Golang中使用Gorm Fiber / argon2.Config》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我正在尝试从 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
}

正确答案


如果您使用包 "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
}

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>