dougaojue8185 2018-08-12 05:15
浏览 233

SHA1秘密编码,等效于PHP hash_hmac

I have the following PHP function

public function encodePassword($raw, $salt)
    {
        return hash_hmac('sha1', $raw . $salt, $this->secret);
    }

which I need to translate to Go. I found the following example, but it doesn't involve secret key. https://gobyexample.com/sha1-hashes

How can I create a function in Go, that produces exactly same result as PHP's hash_hmac?

Update: After Leo's answer, found this resource with hmac examples in many languages: https://github.com/danharper/hmac-examples. Can be useful to somebody.

  • 写回答

2条回答 默认 最新

  • dongsechuan0535 2018-08-12 06:52
    关注

    Something like this:

    import "crypto/sha1"
    import "crypto/hmac"
    
    func hash_hmac_sha1(password, salt, key []byte) []byte {
        h := hmac.New(sha1.New, key)
        h.Write(password)
        h.Write(salt)
        return h.Sum(nil)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!