I'm using MongoDB for my Go and Rails projects (using same database) and i have a bson.Binary data in my document (contain Base64 encoded publicKey)
type Device struct {
Id string `json:"id" form:"id" bson:"_id"`
PublicKey bson.Binary `json:"pub_key" form:"pub_key" bson:"public_key"`
Token string `json:"token" form:"token" bson:"token"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}
but when i retrieve it in my Go projects and the data is somehow corrupted (4 characters are missing)
device := models.Device{}
err = db.C(models.CollectionDevice).Find(bson.M{"_id": deviceId}).One(&device)
publicKey := device.PublicKey
publicKeyBase64 := base64.StdEncoding.EncodeToString(publicKey.Data)
fmt.Println("publicKey BinaryData: ", publicKey.Data)
t
"pub_key": {
"Kind": 0,
"Data": "Axj5A9BgkKJohGh/0BAQEFAAMBjYAAMYmCAYGACqW0smeNiaIgRgXJv7dLZ0n5gTeXxiI9q6h8EdbiPDLiFsv7Jgatwxm+OpucrUBp4sZp/othrSQWnJQR5vpnCZYZnNJUdjTAYw+PyjQ2qq9YEeLHMnMzhYgaq2ata+CSsCjalkOyzLt/rDEBn5WHaCfYm0Vm+QbbEWVLltUqcjvScOyAwEAAQn"
}
(220 characters length in Data)
the original Base64 encoded string is
Axj5A9BgkKJohGh/0BAQEFAAMBjYAAMYmCAYGACqW0smeNiaIgRgXJv7dLZ0 5gTeXxiI9q6h8EdbiPDLiFsv7Jgatwxm+OpucrUBp4sZp/othrSQWnJQR5vp CZYZnNJUdjTAYw+PyjQ2qq9YEeLHMnMzhYgaq2ata+CSsCjalkOyzLt/rDEB 5WHaCfYm0Vm+QbbEWVLltUqcjvScOyAwEAAQ
with 224 characters
i use this code in my Go projects:
fmt.Println("publicKey: ", publicKey.Data)
//publicKey is bson.Binary data type
i have tried to retrieve the data from my rails projects (same database) and it retrieve correctly
public_key.as_json["$binary"]
the result is exactly right (224 characters) :
"Axj5A9BgkKJohGh/0BAQEFAAMBjYAAMYmCAYGACqW0smeNiaIgRgXJv7dLZ0
n5gTeXxiI9q6h8EdbiPDLiFsv7Jgatwxm+OpucrUBp4sZp/othrSQWnJQR5v
pnCZYZnNJUdjTAYw+PyjQ2qq9YEeLHMnMzhYgaq2ata+CSsCjalkOyzLt/rD
EBn5WHaCfYm0Vm+QbbEWVLltUqcjvScOyAwEAAQn
"
As you can see, there is still a at the last of string. Any one know why 4 characters are missing in Go ?
===== Additional information =====
When i store it in my mongoDB from my Rails API, i receive public_key params as Base64 format, but i decode it to binary and then i stored it with this code
def create
params = device_params
public_key = Base64.decode64 device_params[:public_key]
params[:public_key] = BSON::Binary.new(public_key, :generic)
device = Device.find_or_create_by(id: device_params[:id])
render_success device.update_attributes(params), device
end
Your original data contains exactly 4 newline characters :
Axj5A9BgkKJohGh/0BAQEFAAMBjYAAMYmCAYGACqW0smeNiaIgRgXJv7dLZ0
5gTeXxiI9q6h8EdbiPDLiFsv7Jgatwxm+OpucrUBp4sZp/othrSQWnJQR5vp
CZYZnNJUdjTAYw+PyjQ2qq9YEeLHMnMzhYgaq2ata+CSsCjalkOyzLt/rDEB
5WHaCfYm0Vm+QbbEWVLltUqcjvScOyAwEAAQ
These newline characters are not part of the Base64 encoded data, they are just to format the input.
Problem arises likely when you try to store this as a raw string literal, because this is an interpreted string literal, the sequences are to be discarded / removed.
Since the backslash is an invalid symbol for "normal" Base64, they are removed, though the subsequent n
is not (because n
is valid in Base64). As a result, you get corrupted Base64 data.
You should either completely remove the line endings and then you get a valid Base64 which you may store either as a string or binary; or store it as a general string which will preserve the line endings (which have to be dealt with during base64 decoding).