Skip to content

Commit

Permalink
feat: add base62 flag to enable shorter keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajnasz committed May 14, 2024
1 parent 49a272a commit 50f1d68
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BUILD_ARGS=-trimpath -ldflags '-w -s'
all: clean linux

run:
@cd cmd/sekret.link && POSTGRES_URL="postgres://postgres:password@localhost:5432/sekret_link_test?sslmode=disable" go run . -webExternalURL=/api
@cd cmd/sekret.link && POSTGRES_URL="postgres://postgres:password@localhost:5432/sekret_link_test?sslmode=disable" go run . -webExternalURL=/api -base62

build/${BINARY_NAME}.linux.amd64:
cd cmd/sekret.link && GOARCH=amd64 GOOS=linux go build ${BUILD_ARGS} -ldflags "-w -s -X main.version=${VERSION} -X main.build=${BUILD}" -o ../../$@
Expand Down
7 changes: 7 additions & 0 deletions cmd/sekret.link/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/Ajnasz/sekret.link/api"
"github.com/Ajnasz/sekret.link/internal/config"
"github.com/Ajnasz/sekret.link/internal/durable"
"github.com/Ajnasz/sekret.link/internal/key"
"github.com/Ajnasz/sekret.link/internal/models"
"github.com/Ajnasz/sekret.link/internal/models/migrate"
"github.com/Ajnasz/sekret.link/internal/services"
Expand Down Expand Up @@ -123,13 +124,15 @@ func getConfig(ctx context.Context) (*api.HandlerConfig, error) {
postgresDB string
maxDataSize int64
queryVersion bool
base62Encoding bool
)
flag.StringVar(&externalURLParam, "webExternalURL", "", "Web server external url")
flag.StringVar(&postgresDB, "postgresDB", "", "Connection string for postgresql database backend")
flag.IntVar(&expireSeconds, "expireSeconds", 60*60*24*7, "Default expiration time in seconds")
flag.IntVar(&maxExpireSeconds, "maxExpireSeconds", 60*60*24*30, "Max expiration time in seconds")
flag.Int64Var(&maxDataSize, "maxDataSize", 1024*1024, "Max data size")
flag.BoolVar(&queryVersion, "version", false, "Get version information")
flag.BoolVar(&base62Encoding, "base62", false, "Use base62 encoding")
flag.Parse()

if queryVersion {
Expand All @@ -143,6 +146,10 @@ func getConfig(ctx context.Context) (*api.HandlerConfig, error) {
return nil, err
}

if base62Encoding {
key.SetEncodingType(key.Base62Encoding)
}

handlerConfig := api.HandlerConfig{
ExpireSeconds: expireSeconds,
MaxExpireSeconds: maxExpireSeconds,
Expand Down
17 changes: 17 additions & 0 deletions internal/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ var ErrorInvalidKey = errors.New("invalid key")
// SizeAES256 the byte size required for aes 256 encoding
const SizeAES256 int = 32

type encoding int

const (
HexEncoding encoding = iota
Base62Encoding
)

var base62Encoder *basex.Encoding
var encodingType encoding

func init() {
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down Expand Up @@ -96,6 +104,10 @@ func (k *Key) toHex() string {

// String returns the key as a string
func (k *Key) String() string {
if encodingType == Base62Encoding {
return k.toBase62()
}

return k.toHex()
}

Expand Down Expand Up @@ -148,3 +160,8 @@ func FromHex(s string) (*Key, error) {

return k, nil
}

// SetEncodingType sets the encoding type
func SetEncodingType(encoding encoding) {
encodingType = encoding
}

0 comments on commit 50f1d68

Please sign in to comment.