0

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 (
    "fmt"
    b64 "encoding/base64"
)

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

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.

3
  • 1
    new Buffer('test', 'base64') decodes “test” as base64. Did you mean new Buffer('test', 'ascii').toString('base64')? Commented Aug 1, 2018 at 6:30
  • Ooh well now that's interesting; the string "test" is a jwt signing key. And the software in question appears then to be using "thingsboardDefaultSigningKey" as a b64 string. Thanks for your comment, you may have answered my question. Back in a minute! Commented Aug 1, 2018 at 6:33
  • @Ry-, thanks a lot, icza has posted the same in an answer, I'm going to award the tick to that. Sorry I can't split it. Commented Aug 1, 2018 at 6:36

1 Answer 1

5

This constructor:

new Buffer('test', 'base64')

Decodes the input string test, using base64 encoding. It does not encode the test using base64. See the reference:

new Buffer(string[, encoding])
  • string String to encode.
  • encoding The encoding of string. Default: 'utf8'.

The equivalent Go code would be:

data, err := base64.StdEncoding.DecodeString("test")
if err != nil {
    panic(err)
}
fmt.Printf("% x", data)

Which outputs (try it on the Go Playground):

b5 eb 2d

To encode in Node.js, use (for details see How to do Base64 encoding in node.js?):

Buffer.from("test").toString('base64')
Sign up to request clarification or add additional context in comments.

1 Comment

You've very thoroughly answered the question, you can stop editing! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.