diff --git a/Makefile b/Makefile index c28271c..b67b33f 100644 --- a/Makefile +++ b/Makefile @@ -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 ../../$@ diff --git a/cmd/sekret.link/main.go b/cmd/sekret.link/main.go index 8b6a16e..08846e7 100644 --- a/cmd/sekret.link/main.go +++ b/cmd/sekret.link/main.go @@ -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" @@ -123,6 +124,7 @@ 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") @@ -130,6 +132,7 @@ func getConfig(ctx context.Context) (*api.HandlerConfig, error) { 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 { @@ -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, diff --git a/internal/key/key.go b/internal/key/key.go index a3fb02d..ac2df4a 100644 --- a/internal/key/key.go +++ b/internal/key/key.go @@ -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" @@ -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() } @@ -148,3 +160,8 @@ func FromHex(s string) (*Key, error) { return k, nil } + +// SetEncodingType sets the encoding type +func SetEncodingType(encoding encoding) { + encodingType = encoding +}