为什么我的 crypt 包给我无效的魔法前缀错误?

Why does my crypt package give me invalid magic prefix error?

我有以下代码:

import "github.com/kless/osutil/user/crypt/sha512_crypt"
c := sha512_crypt.New()
hash, err := c.Generate([]byte("enter-new-password"), []byte("a$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2"))
if err != nil {
    panic(err)
}

并且产生了以下错误

http: panic serving 192.168.0.16:56730: invalid magic prefix

为什么会发生这种情况,我该如何解决?

Why does this happen and how do I resolve it?


您的魔法前缀无效。


github.com/tredoe/osutil/user/crypt/sha512_crypt/sha512_crypt.go

if !bytes.HasPrefix(salt, c.Salt.MagicPrefix) {
  return "", common.ErrSaltPrefix
}

阅读crypt包代码。


PHP: crypt — One-way string hashing

PHP: password_hash — Creates a password hash

阅读 PHP 文档。

查看您之前的问题:


提供有效的魔法前缀。


例如,

package main

import (
    "fmt"

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

func main() {
    c := sha512_crypt.New()
    magic := sha512_crypt.MagicPrefix
    hash, err := c.Generate(
        []byte("enter-new-password"),
        []byte(magic+"a$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2"),
    )
    if err != nil {
        panic(err)
    }
    fmt.Println(hash)
}

输出:

$$.AVE44JRnLFr9TZx3zASJX6V3Uu0jpnrOV6fW1T5NHy3MUKPaJXHGvjooxrAkYsuIL2HwS/sYgzUZ.cg8FTtz/

注意:

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

现在是新位置的别名

import "github.com/tredoe/osutil/user/crypt/sha512_crypt"