使用Ruby从Mongodb读取二进制数据

时间:2015-01-22 11:39:26

标签: ruby node.js mongodb base64

我是一个MEAN.JS应用程序,它使用这一段代码在Mongodb(db版本v2.6.5)中写入二进制数据:

UserSchema.pre('save', function(next) {
if (this.password) {
    this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
    this.password = this.hashPassword(this.password);
}

next(); })

尝试使用ruby从db中读取这个二进制数据(v 2.1.5 with mongo v1.11.1)我得到了base64解码错误:

2.1.5 :001 > Base64.strict_decode64(salt)

`unpack': invalid base64 (ArgumentError)

尝试以二进制模式解压缩 salt 会导致错误的数据:

[239, 191, 189, 239, 191, 189, 90, 239, 191, 189, 46, 120, 8, 239, 191, 189, 45, 239, 191, 189, 20, 221, 150, 239, 191, 189, 239, 191, 189, 57]

30个字符而不是原来的16个

似乎mongo gem将二进制数据读取为UTF-8字符串并将其打破

如何从mongodb中的存储缓冲区恢复原始字符串?

crypto.randomBytes(16).toString('base64')

0 个答案:

没有答案