dakuaz

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2022 License: MIT Imports: 12 Imported by: 1

README

DAKUAZ

다쿠아즈는 base64 인코딩을 활용하는 url safe한 직렬화를 제공하는 간단한 토큰입니다.

구조

type Dakuaz struct {
	identifier  string
	startTime   int64
	duration    int64
	signature   string
	publicData  map[string]string
	privateData map[string]string

	encryptor cipher.AEAD
	secret    string
}

기본적으로 ID와 시작시각, 생존시간, 서명, 공개 데이터가 존재합니다.

ID는 사용자를 식별하는 데에 사용되고 시작시각과 생존시간을 통해 유효 시간을 파악합니다.
시작시각은 유닉스 타임 밀리초이며 생존시간도 밀리초로 표현합니다.

서명은 hmac + sha256로 이루어집니다. 서명 외의 모든 요소가 들어가기때문에 데이터 무결성을 검증하는 데에 쓰입니다.

공개 데이터는 키-값 문자열로 이루어집니다. 이 값은 모두 암호화되지 않고 직렬화되므로 정보 유출에 주의해야합니다.

비공개 데이터는 키-값 문자열로 이루어집니다. 이 값은 모두 암호화되어 저장되고 받은 쪽에서 Verify 메서드로 검증해야 접근할 수 있습니다.

메서드

func New(password, identifier string, duration time.Duration) *Dakuaz

New 함수는 비밀번호와 ID, 생존시간을 입력받아 새로운 다쿠아즈 객체를 반환합니다.

func (d *Dakuaz) PublicSet(key string, value string) {
	d.publicData[key] = value
}

func (d *Dakuaz) PublicGet(key string) string {
	return d.publicData[key]
}

PublicGetPublicSet으로 공개 데이터의 값에 접근할 수 있습니다.

func (d *Dakuaz) PrivateSet(key string, value string)

func (d *Dakuaz) PrivateGet(key string) string

PrivateGetPrivateSet으로 비공개 데이터의 값에 접근할 수 있습니다.

func (d *Dakuaz) MakeSignature(key string)

MakeSignature 메서드로 입력받은 문자열을 기반으로 서명하여 등록할 수 있습니다.

func (d *Dakuaz) Serialize() string

Serialize 메서드로 앞서 구성한 다쿠아즈 객체를 hex 인코딩으로 직렬화합니다.

func (d *Dakuaz) IsExpired() bool

IsExpired 메서드로 현재 시각과 비교하여 다쿠아즈 객체의 유효기간이 유효한지 검사합니다.

func Desirialize(s string) (*Dakuaz, error)

마지막으로 Desirialize 메서드는 입력받은 hex 문자열에서 그에 해당하는 다쿠아즈 객체를 역직렬화하여 반환합니다.
만약 적절하지 않은 문자열일 경우 에러로 그 이유를 반환합니다.

예시

package main

import (
	"fmt"
	"time"

	"github.com/snowmerak/dakuaz"
)

func main() {
	a, err := dakuaz.New("secret", "snowmerak", time.Minute*30)
	if err != nil {
		panic(err)
	}
	a.PublicSet("age", "28")
	a.PublicSet("loc", "tokyo")
	a.PrivateSet("real", "kym")
	a.MakeSignature()

	fmt.Println(a)
	fmt.Println(a.Serialize())

	b, err := dakuaz.Desirialize(a.Serialize())
	if err != nil {
		panic(err)
	}

	fmt.Println(b)
	fmt.Println(b.Serialize())
	fmt.Println(b.Verify("secret"))
	fmt.Println(b.IsExpired())

	fmt.Println(b.PublicGet("age"))
	fmt.Println(b.PublicGet("loc"))
	fmt.Println(b.PrivateGet("real"))
}
&{snowmerak 1641873381850 1800000 pIVmYciRoLhr2jMKqqnNqqhcgDXgdhPhDNuGoarWdf8 map[age:28 loc:tokyo] map[real:�-�{�Q!��&o�d[��UQ�,�p���] 0xc000020040 secret}
c25vd21lcmFr.AAABfkdHmdo.AAAAAAAbd0A.YWdl_Mjg-bG9j_dG9reW8.cmVhbA_6C3ue5P8CFEhyRWRJh9vvmRbBPrBVVG9LP9wFKrc1g.pIVmYciRoLhr2jMKqqnNqqhcgDXgdhPhDNuGoarWdf8
&{snowmerak 1641873381850 1800000 pIVmYciRoLhr2jMKqqnNqqhcgDXgdhPhDNuGoarWdf8 map[age:28 loc:tokyo] map[real:�-�{�Q!��&o�d[��UQ�,�p���] <nil> }
c25vd21lcmFr.AAABfkdHmdo.AAAAAAAbd0A.YWdl_Mjg-bG9j_dG9reW8.cmVhbA_6C3ue5P8CFEhyRWRJh9vvmRbBPrBVVG9LP9wFKrc1g.pIVmYciRoLhr2jMKqqnNqqhcgDXgdhPhDNuGoarWdf8
<nil>
false
28
tokyo
kym

직렬화 전후와 역직렬화 전후가 동일함을 확인할 수 있습니다.

Verify 메서드를 통한 검증도 통과하였으며 유효기간이 지나지 않았으므로 false과 반환되었습니다.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dakuaz

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

func Desirialize

func Desirialize(s string) (*Dakuaz, error)

func New

func New(secret, identifier string, duration time.Duration) (*Dakuaz, error)

func (*Dakuaz) IsExpired

func (d *Dakuaz) IsExpired() bool

func (*Dakuaz) MakeSignature

func (d *Dakuaz) MakeSignature()

func (*Dakuaz) PrivateGet

func (d *Dakuaz) PrivateGet(key string) string

func (*Dakuaz) PrivateSet

func (d *Dakuaz) PrivateSet(key string, value string)

func (*Dakuaz) PublicGet

func (d *Dakuaz) PublicGet(key string) string

func (*Dakuaz) PublicSet

func (d *Dakuaz) PublicSet(key string, value string)

func (*Dakuaz) Serialize

func (d *Dakuaz) Serialize() string

func (*Dakuaz) Verify

func (d *Dakuaz) Verify(key string) error

Jump to

Keyboard shortcuts

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