util

package
v0.0.0-...-325de3d Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package idutil implements utility functions for generating unique, randomized ids.

Package uuid provides implementation of Universally Unique Identifier (UUID). Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and version 2 (as specified in DCE 1.1).

Index

Constants

View Source
const (
	VariantNCS = iota
	VariantRFC4122
	VariantMicrosoft
	VariantFuture
)

UUID layout variants.

View Source
const (
	DomainPerson = iota
	DomainGroup
	DomainOrg
)

UUID DCE domains.

Variables

View Source
var (
	NamespaceDNS, _  = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	NamespaceURL, _  = FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
	NamespaceOID, _  = FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
	NamespaceX500, _ = FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)

Predefined namespace UUIDs.

View Source
var Nil = UUID{}

The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte, iv []byte, unPaddingFunc func([]byte) []byte) ([]byte, error)

AesDecrypt AesDecrypt

func AesDecryptPkcs5

func AesDecryptPkcs5(crypted []byte, key []byte, iv []byte) ([]byte, error)

AesDecryptPkcs5 解密

func AesDecryptPkcs7

func AesDecryptPkcs7(crypted []byte, key []byte, iv []byte) ([]byte, error)

AesDecryptPkcs7 解密

func AesDecryptPkcs7Base64

func AesDecryptPkcs7Base64(crypted []byte, key []byte, iv []byte) ([]byte, error)

AesDecryptPkcs7Base64 解密

func AesDecryptSimple

func AesDecryptSimple(crypted []byte, key string, iv string) ([]byte, error)

AesDecryptSimple 解密

func AesEncrypt

func AesEncrypt(origData []byte, key []byte, iv []byte, paddingFunc func([]byte, int) []byte) ([]byte, error)

AesEncrypt AesEncrypt

func AesEncryptPkcs5

func AesEncryptPkcs5(origData []byte, key []byte, iv []byte) ([]byte, error)

AesEncryptPkcs5 加密PKCS5

func AesEncryptPkcs7

func AesEncryptPkcs7(origData []byte, key []byte, iv []byte) ([]byte, error)

AesEncryptPkcs7 加密PKCS5

func AesEncryptPkcs7Base64

func AesEncryptPkcs7Base64(origData []byte, key []byte, iv []byte) ([]byte, error)

AesEncryptPkcs7Base64 加密PKCS5

func AesEncryptSimple

func AesEncryptSimple(origData []byte, key string, iv string) ([]byte, error)

AesEncryptSimple 加密

func AnyToDecimal

func AnyToDecimal(num string, n int) int64

AnyToDecimal 任意进制转10进制

func ArrayContains

func ArrayContains(items []string, target string) bool

ArrayContains ArrayContains

func BoolToInt

func BoolToInt(b bool) int

BoolToInt bool值转换为int值

func DecimalToAny

func DecimalToAny(num int64, n int) string

DecimalToAny 10进制转任意进制

func Equal

func Equal(u1 UUID, u2 UUID) bool

Equal returns true if u1 and u2 equals, otherwise returns false.

func FormatSlots

func FormatSlots(slots []uint32) string

FormatSlots FormatSlots

func GenUUID

func GenUUID() string

GenUUID 生成uuid

func GenerUUID

func GenerUUID() string

GenerUUID 生成uuid

func GetCurve25519Key

func GetCurve25519Key(private, public [32]byte) (Key [32]byte)

GetCurve25519Key GetCurve25519Key

func GetCurve25519KeypPair

func GetCurve25519KeypPair() (Aprivate, Apublic [32]byte)

GetCurve25519KeypPair GetCurve25519KeypPair

func GetExternalIP

func GetExternalIP() (string, error)

GetExternalIP 获取本服务器的外网IP

func GetIntranetIP

func GetIntranetIP() (ips []string, err error)

GetIntranetIP 获取本机IP

func GetRandomString

func GetRandomString(num int) string

GetRandomString 生成随机字符串

func GetSlotNum

func GetSlotNum(slotCount int, v string) uint32

GetSlotNum GetSlotNum

func HashCrc32

func HashCrc32(str string) uint32

HashCrc32 通过字符串获取32位数字

func IntToBool

func IntToBool(b int) bool

IntToBool int值转换为bool

func IsIntranet

func IsIntranet(ipStr string) bool

IsIntranet IsIntranet

func JSONToMap

func JSONToMap(json string) (map[string]interface{}, error)

JSONToMap JsonToMap

func JsonToMap

func JsonToMap(json string) (map[string]interface{}, error)

func MD5

func MD5(str string) string

MD5 加密

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

PKCS5Padding PKCS5Padding

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

PKCS5UnPadding PKCS5UnPadding

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

PKCS7Padding PKCS7Padding

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) []byte

PKCS7UnPadding PKCS7UnPadding

func PareTimeStrForYYYYMMdd

func PareTimeStrForYYYYMMdd(timeStr string) (time.Time, error)

func PareTimeStrForYYYY_mm_dd

func PareTimeStrForYYYY_mm_dd(timeStr string) (time.Time, error)

func RSADecrypt

func RSADecrypt(ciphertext string, privateKey string) ([]byte, error)

RSADecrypt RSA 解码

func RSAEncrypt

func RSAEncrypt(data string, publicKey string) ([]byte, error)

RSAEncrypt RSA编码

func ReadJSONByByte

func ReadJSONByByte(body []byte, obj interface{}) error

ReadJSONByByte 读取JSON

func ReadJsonByByte

func ReadJsonByByte(body []byte, obj interface{}) error

func RemoveRepeatedElement

func RemoveRepeatedElement(arr []string) (newArr []string)

func SlotsContains

func SlotsContains(b, subslice []byte) bool

SlotsContains SlotsContains

func ToJSON

func ToJSON(obj interface{}) string

ToJSON 将对象转换为JSON

func ToJson

func ToJson(obj interface{}) string

将对象转换为JSON

func ToyyyyMM2

func ToyyyyMM2(tm time.Time) string

func ToyyyyMMdd

func ToyyyyMMdd(tm time.Time) string

func ToyyyyMMddHHmm

func ToyyyyMMddHHmm(tm time.Time) string

func ToyyyyMMddHHmmss

func ToyyyyMMddHHmmss(tm time.Time) string

func Toyyyy_MM

func Toyyyy_MM(tm time.Time) string

func Toyyyy_MM_dd

func Toyyyy_MM_dd(tm time.Time) string

带-的日期

Types

type Generator

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

Generator generates unique identifiers based on counters, timestamps, and a node member ID.

The initial id is in this format: High order 2 bytes are from memberID, next 5 bytes are from timestamp, and low order one byte is a counter. | prefix | suffix | | 2 bytes | 5 bytes | 1 byte | | memberID | timestamp | cnt |

The timestamp 5 bytes is different when the machine is restart after 1 ms and before 35 years.

It increases suffix to generate the next id. The count field may overflow to timestamp field, which is intentional. It helps to extend the event window to 2^56. This doesn't break that id generated after restart is unique because etcd throughput is << 256req/ms(250k reqs/second).

func NewGenerator

func NewGenerator(memberID uint16, now time.Time) *Generator

func (*Generator) Next

func (g *Generator) Next() uint64

Next generates a id that is unique.

type NullUUID

type NullUUID struct {
	UUID  UUID
	Valid bool
}

NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database

func (*NullUUID) Scan

func (u *NullUUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (NullUUID) Value

func (u NullUUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type SlotBitMap

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

SlotBitMap SlotBitMap

func NewSlotBitMap

func NewSlotBitMap(slotNum int) *SlotBitMap

NewSlotBitMap NewSlotBitMap

func NewSlotBitMapWithBits

func NewSlotBitMapWithBits(bits []byte) *SlotBitMap

NewSlotBitMapWithBits NewSlotBitMapWithBits

func (*SlotBitMap) CleanSlots

func (s *SlotBitMap) CleanSlots(slots []byte)

CleanSlots CleanSlots

func (*SlotBitMap) ExportSlots

func (s *SlotBitMap) ExportSlots(num int) []byte

ExportSlots ExportSlots

func (*SlotBitMap) GetBits

func (s *SlotBitMap) GetBits() []byte

GetBits GetBits

func (*SlotBitMap) GetSlot

func (s *SlotBitMap) GetSlot(num uint) bool

GetSlot GetSlot

func (*SlotBitMap) GetVaildSlotNum

func (s *SlotBitMap) GetVaildSlotNum() int

GetVaildSlotNum GetVaildSlotNum

func (*SlotBitMap) GetVaildSlots

func (s *SlotBitMap) GetVaildSlots() []uint32

GetVaildSlots GetVaildSlots

func (*SlotBitMap) MergeSlots

func (s *SlotBitMap) MergeSlots(bs ...[]byte)

MergeSlots MergeSlots

func (*SlotBitMap) Reset

func (s *SlotBitMap) Reset()

Reset Reset

func (*SlotBitMap) SetSlot

func (s *SlotBitMap) SetSlot(num uint32, v bool)

SetSlot SetSlot

func (*SlotBitMap) SetSlotForRange

func (s *SlotBitMap) SetSlotForRange(start, end uint32, v bool)

SetSlotForRange SetSlotForRange

type UUID

type UUID [16]byte

UUID representation compliant with specification described in RFC 4122.

func And

func And(u1 UUID, u2 UUID) UUID

And returns result of binary AND of two UUIDs.

func FromBytes

func FromBytes(input []byte) (u UUID, err error)

FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.

func FromBytesOrNil

func FromBytesOrNil(input []byte) UUID

FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.

func FromString

func FromString(input string) (u UUID, err error)

FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.

func FromStringOrNil

func FromStringOrNil(input string) UUID

FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.

func NewV1

func NewV1() UUID

NewV1 returns UUID based on current timestamp and MAC address.

func NewV2

func NewV2(domain byte) UUID

NewV2 returns DCE Security UUID based on POSIX UID/GID.

func NewV3

func NewV3(ns UUID, name string) UUID

NewV3 returns UUID based on MD5 hash of namespace UUID and name.

func NewV4

func NewV4() UUID

NewV4 returns random generated UUID.

func NewV5

func NewV5(ns UUID, name string) UUID

NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.

func Or

func Or(u1 UUID, u2 UUID) UUID

Or returns result of binary OR of two UUIDs.

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns bytes slice representation of UUID.

func (UUID) MarshalBinary

func (u UUID) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (UUID) MarshalText

func (u UUID) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

func (*UUID) Scan

func (u *UUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface. A 16-byte slice is handled by UnmarshalBinary, while a longer byte slice or a string is handled by UnmarshalText.

func (*UUID) SetVariant

func (u *UUID) SetVariant()

SetVariant sets variant bits as described in RFC 4122.

func (*UUID) SetVersion

func (u *UUID) SetVersion(v byte)

SetVersion sets version bits.

func (UUID) String

func (u UUID) String() string

Returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

func (*UUID) UnmarshalBinary

func (u *UUID) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long.

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported: "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

func (UUID) Variant

func (u UUID) Variant() uint

Variant returns UUID layout variant.

func (UUID) Version

func (u UUID) Version() uint

Version returns algorithm version used to generate UUID.

Jump to

Keyboard shortcuts

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