在Go中生成crypt()sha512哈希 [英] Generating crypt() sha512 hashes in Go

查看:109
本文介绍了在Go中生成crypt()sha512哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GoLang中的授权模块.在我们将PHP5与crypt函数一起使用之前.哈希像SHA-512一样生成:

I am working on my authorization module in GoLang. Before we used PHP5 with the crypt function. The hash was generated like SHA-512:

$6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21

并像这样存储在数据库中.但是现在我需要使其在GoLang中也能正常工作.我在Google上搜索过,并尝试了不同的操作,例如:

And stored like that in the database. But now I need make it work also in GoLang. I have searched on Google and tried different things, such as:

t512 := sha512_crypt.Crypt("rasmuslerdorf", "$6$usesomesillystringforsalt$")
fmt.Printf("hash: %v\n", t512)

但是所有人都会产生不同的东西.谁能进一步帮助我们?

But all generate different things. Who can help us further?

我们要验证并创建类似于php版本的哈希.

We want validate and create hashes like the php version.

谢谢.

推荐答案

位于osutil 库> https://github.com/kless/osutil 支持所有 crypt()哈希类型.

The osutil library at https://github.com/kless/osutil has support for all crypt() hash types.

您的密码哈希可以使用以下php代码生成:

Your password hash can be produced with the following php code:

echo crypt('rasmuslerdorf', '$6$usesomesillystringforsalt');

此代码产生以下哈希:

$6$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21

可以在Go中复制这样的内容:

This can be reproduced in Go like this:

package main                                                

import (
    "fmt"

    "github.com/kless/osutil/user/crypt/sha512_crypt"
)

func main() {
    c := sha512_crypt.New()
    hash, err := c.Generate([]byte("rasmuslerdorf"), []byte("$6$usesomesillystringforsalt"))
    if err != nil {
        panic(err)
    }

    fmt.Println(hash)
}

运行时,它还会产生正确的哈希值:

When run, it also produces the correct hash:

$6$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21

我希望这能回答您的问题.

I hope this answers your question.

在实施此操作时,请注意,盐中仅使用16个字符,因此盐 usesomesillystri 返回相同的哈希值.确保在生产代码中选择随机盐.

While implementing this please note that only 16 characters are used from the salt, so the same hash is returned for the salt usesomesillystri. Make sure that you choose random salts in the production code.

这篇关于在Go中生成crypt()sha512哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆