2018-08-01 06:20
浏览 71

golang base64编码vs nodejs缓冲区base64编码

I'm using a piece of software that base64 encodes in node as follows:

const enc = new Buffer('test', 'base64')

console.log(enc) displays:

<Buffer b5 eb 2d>

I'm writing a golang service that needs to interoperate with this. But I can't reproduce the above result in go.

package main

import (
    b64 "encoding/base64"

func main() {
    // Attempt 1
    res := []byte(b64.URLEncoding.EncodeToString([]byte("test")))
    // Attempt 2
    buf := make([]byte, 8)
    b64.URLEncoding.Encode(buf, []byte("test"))

The above prints:

[100 71 86 122 100 65 61 61]
[100 71 86 122 100 65 61 61]

both of which are rather different from node's output. I suspect the difference is that node is storing the string as bytes from a base64 string, while go is storing the string as bytes from an ascii/utf8 string represented as base64. But haven't figured out how to get go to do as node is doing!

I skimmed the go source for the encoding, then attempted to find the Node source for Buffer, but after a little while hunting decided it would probably be much quicker to post here in the hope someone knew the answer off-hand.

1条回答 默认 最新

相关推荐 更多相似问题