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
- Variables
- func AesDecrypt(crypted, key []byte, iv []byte, unPaddingFunc func([]byte) []byte) ([]byte, error)
- func AesDecryptPkcs5(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptPkcs7(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptPkcs7Base64(crypted []byte, key []byte, iv []byte) ([]byte, error)
- func AesDecryptSimple(crypted []byte, key string, iv string) ([]byte, error)
- func AesEncrypt(origData []byte, key []byte, iv []byte, paddingFunc func([]byte, int) []byte) ([]byte, error)
- func AesEncryptPkcs5(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptPkcs7(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptPkcs7Base64(origData []byte, key []byte, iv []byte) ([]byte, error)
- func AesEncryptSimple(origData []byte, key string, iv string) ([]byte, error)
- func AnyToDecimal(num string, n int) int64
- func ArrayContains(items []string, target string) bool
- func BoolToInt(b bool) int
- func DecimalToAny(num int64, n int) string
- func Equal(u1 UUID, u2 UUID) bool
- func FormatSlots(slots []uint32) string
- func GenUUID() string
- func GenerUUID() string
- func GetCurve25519Key(private, public [32]byte) (Key [32]byte)
- func GetCurve25519KeypPair() (Aprivate, Apublic [32]byte)
- func GetExternalIP() (string, error)
- func GetIntranetIP() (ips []string, err error)
- func GetRandomString(num int) string
- func GetSlotNum(slotCount int, v string) uint32
- func HashCrc32(str string) uint32
- func IntToBool(b int) bool
- func IsIntranet(ipStr string) bool
- func JSONToMap(json string) (map[string]interface{}, error)
- func JsonToMap(json string) (map[string]interface{}, error)
- func MD5(str string) string
- func PKCS5Padding(ciphertext []byte, blockSize int) []byte
- func PKCS5UnPadding(origData []byte) []byte
- func PKCS7Padding(ciphertext []byte, blockSize int) []byte
- func PKCS7UnPadding(origData []byte) []byte
- func PareTimeStrForYYYYMMdd(timeStr string) (time.Time, error)
- func PareTimeStrForYYYY_mm_dd(timeStr string) (time.Time, error)
- func RSADecrypt(ciphertext string, privateKey string) ([]byte, error)
- func RSAEncrypt(data string, publicKey string) ([]byte, error)
- func ReadJSONByByte(body []byte, obj interface{}) error
- func ReadJsonByByte(body []byte, obj interface{}) error
- func RemoveRepeatedElement(arr []string) (newArr []string)
- func SlotsContains(b, subslice []byte) bool
- func ToJSON(obj interface{}) string
- func ToJson(obj interface{}) string
- func ToyyyyMM2(tm time.Time) string
- func ToyyyyMMdd(tm time.Time) string
- func ToyyyyMMddHHmm(tm time.Time) string
- func ToyyyyMMddHHmmss(tm time.Time) string
- func Toyyyy_MM(tm time.Time) string
- func Toyyyy_MM_dd(tm time.Time) string
- type Generator
- type NullUUID
- type SlotBitMap
- func (s *SlotBitMap) CleanSlots(slots []byte)
- func (s *SlotBitMap) ExportSlots(num int) []byte
- func (s *SlotBitMap) GetBits() []byte
- func (s *SlotBitMap) GetSlot(num uint) bool
- func (s *SlotBitMap) GetVaildSlotNum() int
- func (s *SlotBitMap) GetVaildSlots() []uint32
- func (s *SlotBitMap) MergeSlots(bs ...[]byte)
- func (s *SlotBitMap) Reset()
- func (s *SlotBitMap) SetSlot(num uint32, v bool)
- func (s *SlotBitMap) SetSlotForRange(start, end uint32, v bool)
- type UUID
- func And(u1 UUID, u2 UUID) UUID
- func FromBytes(input []byte) (u UUID, err error)
- func FromBytesOrNil(input []byte) UUID
- func FromString(input string) (u UUID, err error)
- func FromStringOrNil(input string) UUID
- func NewV1() UUID
- func NewV2(domain byte) UUID
- func NewV3(ns UUID, name string) UUID
- func NewV4() UUID
- func NewV5(ns UUID, name string) UUID
- func Or(u1 UUID, u2 UUID) UUID
- func (u UUID) Bytes() []byte
- func (u UUID) MarshalBinary() (data []byte, err error)
- func (u UUID) MarshalText() (text []byte, err error)
- func (u *UUID) Scan(src interface{}) error
- func (u *UUID) SetVariant()
- func (u *UUID) SetVersion(v byte)
- func (u UUID) String() string
- func (u *UUID) UnmarshalBinary(data []byte) (err error)
- func (u *UUID) UnmarshalText(text []byte) (err error)
- func (u UUID) Value() (driver.Value, error)
- func (u UUID) Variant() uint
- func (u UUID) Version() uint
Constants ¶
const ( VariantNCS = iota VariantRFC4122 VariantMicrosoft VariantFuture )
UUID layout variants.
const ( DomainPerson = iota DomainGroup DomainOrg )
UUID DCE domains.
Variables ¶
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.
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 ¶
AesDecrypt AesDecrypt
func AesDecryptPkcs5 ¶
AesDecryptPkcs5 解密
func AesDecryptPkcs7 ¶
AesDecryptPkcs7 解密
func AesDecryptPkcs7Base64 ¶
AesDecryptPkcs7Base64 解密
func AesDecryptSimple ¶
AesDecryptSimple 解密
func AesEncrypt ¶
func AesEncrypt(origData []byte, key []byte, iv []byte, paddingFunc func([]byte, int) []byte) ([]byte, error)
AesEncrypt AesEncrypt
func AesEncryptPkcs5 ¶
AesEncryptPkcs5 加密PKCS5
func AesEncryptPkcs7 ¶
AesEncryptPkcs7 加密PKCS5
func AesEncryptPkcs7Base64 ¶
AesEncryptPkcs7Base64 加密PKCS5
func AesEncryptSimple ¶
AesEncryptSimple 加密
func ArrayContains ¶
ArrayContains ArrayContains
func GetCurve25519Key ¶
GetCurve25519Key GetCurve25519Key
func GetCurve25519KeypPair ¶
func GetCurve25519KeypPair() (Aprivate, Apublic [32]byte)
GetCurve25519KeypPair GetCurve25519KeypPair
func PKCS5Padding ¶
PKCS5Padding PKCS5Padding
func PKCS7Padding ¶
PKCS7Padding PKCS7Padding
func RSADecrypt ¶
RSADecrypt RSA 解码
func ReadJsonByByte ¶
func RemoveRepeatedElement ¶
func ToyyyyMMdd ¶
func ToyyyyMMddHHmm ¶
func ToyyyyMMddHHmmss ¶
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).
type NullUUID ¶
NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database
type SlotBitMap ¶
type SlotBitMap struct {
// contains filtered or unexported fields
}
SlotBitMap SlotBitMap
func NewSlotBitMapWithBits ¶
func NewSlotBitMapWithBits(bits []byte) *SlotBitMap
NewSlotBitMapWithBits NewSlotBitMapWithBits
func (*SlotBitMap) ExportSlots ¶
func (s *SlotBitMap) ExportSlots(num int) []byte
ExportSlots ExportSlots
func (*SlotBitMap) GetVaildSlotNum ¶
func (s *SlotBitMap) GetVaildSlotNum() int
GetVaildSlotNum GetVaildSlotNum
func (*SlotBitMap) GetVaildSlots ¶
func (s *SlotBitMap) GetVaildSlots() []uint32
GetVaildSlots GetVaildSlots
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 FromBytes ¶
FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.
func FromBytesOrNil ¶
FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.
func FromString ¶
FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.
func FromStringOrNil ¶
FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.
func (UUID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (UUID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.
func (*UUID) Scan ¶
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) String ¶
Returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long.
func (*UUID) UnmarshalText ¶
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"