commit 5dd11641656f3e6a848713b6f0e723efb7882fac Author: Darko Luketic Date: Thu Aug 3 17:25:00 2023 +0200 initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..28df167 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.icod.de/dalu/oid64 + +go 1.20 diff --git a/oid64.go b/oid64.go new file mode 100644 index 0000000..7bb7527 --- /dev/null +++ b/oid64.go @@ -0,0 +1,24 @@ +package oid64 + +import ( + "encoding/base64" + "encoding/hex" +) + +func ObjectIDToBase64(oid string) (string, error) { + b, e := hex.DecodeString(oid) + if e != nil { + return "", e + } + s := base64.URLEncoding.EncodeToString(b) + return s, nil +} + +func Base64ToObjectID(bid string) (string, error) { + b, e := base64.URLEncoding.DecodeString(bid) + if e != nil { + return "", e + } + s := hex.EncodeToString(b) + return s, nil +} diff --git a/oid64_test.go b/oid64_test.go new file mode 100644 index 0000000..b134585 --- /dev/null +++ b/oid64_test.go @@ -0,0 +1,31 @@ +package oid64 + +import "testing" + +func TestObjectIDToBase64(t *testing.T) { + in := "507f191e810c19729de860ea" + exOut := "UH8ZHoEMGXKd6GDq" + + out, e := ObjectIDToBase64(in) + if e != nil { + t.Fatal(e.Error()) + } + if out != exOut { + t.Logf("Results don't match. Expected %s got %s", exOut, out) + t.Fail() + } +} + +func TestBase64ToObjectID(t *testing.T) { + in := "UH8ZHoEMGXKd6GDq" + exOut := "507f191e810c19729de860ea" + + out, e := Base64ToObjectID(in) + if e != nil { + t.Fatal(e.Error()) + } + if out != exOut { + t.Logf("Results don't match. Expected %s got %s", exOut, out) + t.Fail() + } +}