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

老子叫甜甜 提交于 2019-12-08 15:36:26

问题


I have the following code:

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

And it produced the following error

http: panic serving 192.168.0.16:56730: invalid magic prefix

Why does this happen and how do I resolve it?


回答1:


Why does this happen and how do I resolve it?


You have an invalid magic prefix.


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

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

Read the crypt package code.


PHP: crypt — One-way string hashing

PHP: password_hash — Creates a password hash

Read the PHP documentation.

See your earlier question: golang equivalent of PHP crypt().


Provide a valid magic prefix.


For example,

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+"$2a$09$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2"),
    )
    if err != nil {
        panic(err)
    }
    fmt.Println(hash)
}

Output:

$6$$.AVE44JRnLFr9TZx3zASJX6V3Uu0jpnrOV6fW1T5NHy3MUKPaJXHGvjooxrAkYsuIL2HwS/sYgzUZ.cg8FTtz/

NOTE:

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

is now an alias for the new location

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


来源:https://stackoverflow.com/questions/51123718/why-does-my-crypt-package-give-me-invalid-magic-prefix-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!