This commit is contained in:
Darko Luketic 2023-08-03 17:25:00 +02:00
commit 5dd1164165
4 changed files with 58 additions and 0 deletions

0
README.md Normal file
View File

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module code.icod.de/dalu/oid64
go 1.20

24
oid64.go Normal file
View File

@ -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
}

31
oid64_test.go Normal file
View File

@ -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()
}
}