Home >Backend Development >Golang >Golang Gorm Fiber/argon2.Config undefined

Golang Gorm Fiber/argon2.Config undefined

王林
王林forward
2024-02-12 21:10:081205browse

Golang Gorm Fiber / argon2.Config 未定义

php editor Baicao may encounter "undefined" problems when using Golang Gorm Fiber and argon2.Config. This problem is mainly caused by the lack of corresponding definition or configuration. To solve this problem, we need to check whether the corresponding libraries and configurations are introduced correctly, and ensure that they are used and called correctly in the code. With careful inspection and debugging, we can resolve this issue and get our code to run properly.

Question content

I'm trying to switch from PHP to GO but I'm stuck and I'm asking for your help.

I'm trying to create a cryptographic hash function using Argon2, but I keep getting the error "Undefined: argon2.Config" when I use "argon2.Config{}". I've recompiled the project multiple times but can't seem to find a solution. I'm asking for your help in resolving this issue.

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
}

Workaround

If you use the package "golang.org/x/crypto/argon2" you can use the argon2.IDKey() method. Here is a working example:

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
}

The above is the detailed content of Golang Gorm Fiber/argon2.Config undefined. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete