【发布时间】:2017-06-14 23:12:39
【问题描述】:
我正在使用 Golang 开发 API,正在使用 Gorm 作为 ORM。目前有一个 []byte 字段的问题,我在我的结构中将其定义为:
type Member struct {
MyField []byte `gorm:"column:MyField" schema:"-"`
}
然后,我有保存和读取该资源的方法,因此我保存它时的值与我正在读取的值不同。例如,我使用 bcrypt 生成哈希密码,尝试使用 12345 结果是:
[36 50 97 36 49 48 36 46 56 98 88 72 82 71 113 66 100 65 105 103 70 119 114 97 73 77 99 78 117 106 54 78 103 88 68 49 56 110 103 112 105 86
104 79 117 47 114 57 116 51 47 53 97 100 109 103 106 46 68 109]
直到一切正常,然后当我从数据库中读取寄存器并打印我得到的值:
[36 50 97 36 48 52 36 56 49 67 66 121 118 90 47 47 104 49 83 120 50 108 112 71 73 51 67 88 46 97 52 74 54 66 84 73 106 105 110 122 98 69 90 51
78 113 67 66 49 103 50 56 116 47 57 120 78 103 109 54]
它们是不同的,为什么? 我使用 gorm 创建表,该列的类型在我的 postgres 数据库中定义为 bytea
代码
要保存:
bs, err := bcrypt.GenerateFromPassword([]byte(Pass), bcrypt.DefaultCost)
if err != nil {
fmt.Println("Hey error!")
}
user.MyField = bs
db.Create(&user)
阅读: db.First(&user,id) fmt.Println(user.MyField)
【问题讨论】:
标签: go hash byte bcrypt go-gorm