Giter Club home page Giter Club logo

Comments (3)

jimmycuadra avatar jimmycuadra commented on June 11, 2024 1

Thanks for the report! I'll take a look at this, but it might be a few days, as I'll be out of town on vacation through next week.

from homeserver.

jimmycuadra avatar jimmycuadra commented on June 11, 2024

The issue here is that base64.NewEncoder writes to the provided buffer in blocks of 4 bytes, and must be manually closed to flush any remaining bytes of less than a block of 4, so you're ending up with only a 30 byte value.

Looks like you can fix it by either calling Close on the encoder, or by using EncodeToString instead:

encoded := base64.StdEncoding.EncodeToString(b)
fmt.Println(encoded)

I think it'd be useful for Ruma's CLI to have a command to easily generate a value suitable for use as the macaroon secret key, so I'm going to leave this issue open to remind me to do that.

from homeserver.

cptaffe avatar cptaffe commented on June 11, 2024

My bad, I should really read the docs more thoroughly. Still bothers me that defer won't work in this context.

Obsoleted by #267dfd9, but here is my patched code:

package main

import (
    "bytes"
    "crypto/rand"
    "encoding/base64"
    "fmt"
    "log"
)

// Macaroon type
type Macaroon []byte

// NewMacaroon returns a random Macaroon
func NewMacaroon() ([]byte, error) {
    b := make([]byte, 32)
    _, err := rand.Read(b)
    if err != nil {
        return nil, err
    }
    buf := bytes.Buffer{}
    w := base64.NewEncoder(base64.StdEncoding, &buf)
    _, err = w.Write(b)
    if err != nil {
        return nil, err
    }
    w.Close()
    return buf.Bytes(), nil
}

func main() {
    m, err := NewMacaroon()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(m))
}

from homeserver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.