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.

icza
  • 289,344
  • 42
  • 658
  • 630
mkingston
  • 2,416
  • 12
  • 24
  • 1
    `new Buffer('test', 'base64')` *decodes* “test” as base64. Did you mean `new Buffer('test', 'ascii').toString('base64')`? – Ry- Aug 01 '18 at 06: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! – mkingston Aug 01 '18 at 06: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. – mkingston Aug 01 '18 at 06:36

1 Answers1

3

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')
Community
  • 1
  • 1
icza
  • 289,344
  • 42
  • 658
  • 630