extid

package module
v0.0.0-...-26c790b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 6 Imported by: 0

README

Go Reference

go-extid

It can be valuable to internally use a serial integer as an ID without revealing that ID to the outside world. go-extid uses AES-128 to convert to and from an external ID that cannot feasibly be decoded without the secret key.

This prevents outsiders from quantifying the usage of your application by observing the rate of increase of IDs as well as provides protection against brute force crawling of all resources.

Example Usage

prefix := "user"
key := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

et, err := extid.NewType(prefix, key)
if err != nil {
  return err
}

et.Encode(1) // => "user_13189a6ae4ab07ae70a3aabd30be99de"

Performance

$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github.com/jackc/go-extid
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkEncode-16    	 8812752	       126.3 ns/op	     112 B/op	       4 allocs/op
BenchmarkDecode-16    	14138239	        78.88 ns/op	      32 B/op	       2 allocs/op
PASS
ok  	github.com/jackc/go-extid	5.298s

Other Implementations

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

type Type struct {
	// contains filtered or unexported fields
}

func NewType

func NewType(prefix string, key []byte) (*Type, error)

func (*Type) Decode

func (t *Type) Decode(s string) (int64, error)

func (*Type) Encode

func (t *Type) Encode(n int64) string
Example
prefix := "user"
key := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

et, err := extid.NewType(prefix, key)
if err != nil {
	fmt.Println(err)
	return
}

for i := 0; i < 5; i++ {
	fmt.Println(i, et.Encode(int64(i)))
}
Output:

0 user_c6a13b37878f5b826f4f8162a1c8d879
1 user_13189a6ae4ab07ae70a3aabd30be99de
2 user_c76e8fcf7ad0fe9b39e083739cbe26c2
3 user_90cb45611c3105c84624b2ac12cb5b74
4 user_79d0782401799c8fd25121e869b5b532

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL