sutil

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: MIT Imports: 22 Imported by: 3

README

siot-util

An Common Tools Develop By Golang

Documentation

Overview

Package crc implements generic CRC calculations up to 64 bits wide. It aims to be fairly complete, allowing users to match pretty much any CRC algorithm used in the wild by choosing appropriate Parameters. And it's also fairly fast for everyday use.

This package has been largely inspired by Ross Williams' 1993 paper "A Painless Guide to CRC Error Detection Algorithms". A good list of parameter sets for various CRC algorithms can be found at http://reveng.sourceforge.net/crc-catalogue/.

Index

Constants

View Source
const (
	TypeErrSystem   = "服务器错误"
	TypeErrDatabase = "数据库错误"
	TypeErrMq       = "消息服务错误"
	TypeErrCache    = "缓存服务错误"
	TypeErrUser     = "用户操作错误"
)

define Col operations

View Source
const (
	StatusSuccess         = 200
	StatusTypeErrSystem   = 500
	StatusTypeErrDatabase = 600
	StatusTypeErrMq       = 700
	StatusTypeErrCache    = 800
	StatusTypeErrUser     = 400
)

Variables

View Source
var (
	// X-25 CRC parameters, also known as CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
	X25 = &Parameters{Width: 16, Polynomial: 0x1021, Init: 0xFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFF}
	// CCITT CRC parameters
	CCITT = &Parameters{Width: 16, Polynomial: 0x1021, Init: 0xFFFF, ReflectIn: false, ReflectOut: false, FinalXor: 0x0}
	// CRC16 CRC parameters, also known as ARC
	CRC16 = &Parameters{Width: 16, Polynomial: 0x8005, Init: 0x0000, ReflectIn: true, ReflectOut: true, FinalXor: 0x0}
	// CRC16 MODBUS CRC parameters, also known as ARC
	CRC16MODBUS = &Parameters{Width: 16, Polynomial: 0x8005, Init: 0xFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0x0}
	// XMODEM is a set of CRC parameters commonly referred as "XMODEM"
	XMODEM = &Parameters{Width: 16, Polynomial: 0x1021, Init: 0x0000, ReflectIn: false, ReflectOut: false, FinalXor: 0x0}
	// XMODEM2 is another set of CRC parameters commonly referred as "XMODEM"
	XMODEM2 = &Parameters{Width: 16, Polynomial: 0x8408, Init: 0x0000, ReflectIn: true, ReflectOut: true, FinalXor: 0x0}

	// CRC32 is by far the the most commonly used CRC-32 polynom and set of parameters
	CRC32 = &Parameters{Width: 32, Polynomial: 0x04C11DB7, Init: 0xFFFFFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFFFFFF}
	// IEEE is an alias to CRC32
	IEEE = CRC32
	// Castagnoli polynomial. used in iSCSI. And also provided by hash/crc32 package.
	Castagnoli = &Parameters{Width: 32, Polynomial: 0x1EDC6F41, Init: 0xFFFFFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFFFFFF}
	// CRC32C is an alias to Castagnoli
	CRC32C = Castagnoli
	// Koopman polynomial
	Koopman = &Parameters{Width: 32, Polynomial: 0x741B8CD7, Init: 0xFFFFFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFFFFFF}

	// CRC64ISO is set of parameters commonly known as CRC64-ISO
	CRC64ISO = &Parameters{Width: 64, Polynomial: 0x000000000000001B, Init: 0xFFFFFFFFFFFFFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFFFFFFFFFFFFFF}
	// CRC64ECMA is set of parameters commonly known as CRC64-ECMA
	CRC64ECMA = &Parameters{Width: 64, Polynomial: 0x42F0E1EBA9EA3693, Init: 0xFFFFFFFFFFFFFFFF, ReflectIn: true, ReflectOut: true, FinalXor: 0xFFFFFFFFFFFFFFFF}
)
View Source
var (
	ErrDb                  = &ControllerError{600, 10001, "数据库错误", "数据库操作错误", ""}
	ErrDupRecord           = &ControllerError{600, 10002, "数据库错误", "数据库记录重复", ""}
	ErrNoRecord            = &ControllerError{600, 10003, "数据库错误", "数据库记录不存在", ""}
	ErrUserPass            = &ControllerError{600, 10004, "数据库错误", "用户名或密码不正确", ""}
	ErrDbInsert            = &ControllerError{600, 10005, "数据库错误", "数据添加错误", ""}
	ErrDbUpdate            = &ControllerError{600, 10006, "数据库错误", "数据更新错误", ""}
	ErrDbDelete            = &ControllerError{600, 10007, "数据库错误", "数据删除错误", ""}
	ErrChangeAccountStatus = &ControllerError{600, 10008, "数据库错误", "更新账户状态错误", ""}
)
View Source
var (
	ErrUserExist   = &ControllerError{400, 10001, "用户操作错误", "用户账户已存在", ""}
	ErrUserInput   = &ControllerError{400, 10002, "用户操作错误", "用户输入参数错误", ""}
	ErrUserModify  = &ControllerError{400, 10003, "用户操作错误", "修改用户错误", ""}
	ErrUserDelete  = &ControllerError{400, 10004, "用户操作错误", "删除用户错误", ""}
	ErrNoUser      = &ControllerError{400, 10005, "用户操作错误", "用户账户不存在", ""}
	ErrUserGet     = &ControllerError{400, 10006, "用户操作错误", "获取所有用户错误", ""}
	ErrUserGetLogs = &ControllerError{400, 10007, "用户操作错误", "获取用户登录日志错误", ""}

	ErrOpenFile   = &ControllerError{500, 10009, "服务器错误", "打开文件出错", ""}
	ErrWriteFile  = &ControllerError{500, 10010, "服务器错误", "写文件出错", ""}
	ErrSystem     = &ControllerError{500, 10011, "服务器错误", "操作系统错误", ""}
	ErrExpired    = &ControllerError{400, 10012, "登录已过期", "验证token过期", ""}
	ErrPermission = &ControllerError{400, 10013, "没有权限", "没有操作权限", ""}
)
View Source
var (
	Err404          = &ControllerError{StatusTypeErrUser, 404, "page not found", "page not found", ""}
	ErrInputData    = &ControllerError{400, 10001, "数据输入错误", "客户端参数错误", ""}
	ErrVersionCheck = &ControllerError{400, 10001, "数据输入错误", "版本检查错误", ""}
	ErrTransferData = &ControllerError{500, 20001, "数据转换错误", "Json 字符串转 Map 错误", ""}
)
View Source
var (
	Actionsuccess = &ControllerError{200, 90000, "操作成功", "操作成功", ""}
)

Functions

func AESDecrypt

func AESDecrypt(cipherBytes, key, iv []byte) ([]byte, error)

AESDecrypt AES解密

func AesDecrypt

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

func AesEncrypt

func AesEncrypt(origData, key []byte) ([]byte, error)

func Bin

func Bin(i int, prefix bool) string

/////////////////////////////////////////////////

func Bin2int

func Bin2int(binStr string) int

func ByteToFloat32

func ByteToFloat32(bytes []byte) float32

func BytesReverse

func BytesReverse(src []byte) []byte

func BytesToInt

func BytesToInt(b []byte) int64

字节数组转整数

func CalculateCRC

func CalculateCRC(crcParams *Parameters, data []byte) uint64

CalculateCRC implements simple straight forward bit by bit calculation. It is relatively slow for large amounts of data, but does not require any preparation steps. As a result, it might be faster in some cases then building a table required for faster calculation.

func Create_token

func Create_token(appid string, secret string, exp int64) (string, int64)

func DecodeWeAppUserInfo

func DecodeWeAppUserInfo(encryptedData string, sessionKey string, iv string) (string, error)

DecodeWeAppUserInfo 解密微信小程序用户信息

func Encode

func Encode(dst, src []byte) int

func EncodeByteArrayToString

func EncodeByteArrayToString(src []byte) string

func EncodedLen

func EncodedLen(n int) int

func FormatHex

func FormatHex(hexStr string, length int) string

func FormatStr

func FormatStr(str string, length int) string

func GeneratePassHash

func GeneratePassHash(password string, salt string) (hash string, err error)

func GenerateSalt

func GenerateSalt() (salt string, err error)

func GetGuid

func GetGuid() string

func GetMd5String

func GetMd5String(s string) string

生成GUID编码

func GetQrByte

func GetQrByte(chd string, chs int, qrType int) (file []byte)

func GetSerialNumber

func GetSerialNumber() string

func Hex2int

func Hex2int(hexStr string) int

func Hex2int16

func Hex2int16(hexStr string) int

func Hex2int64

func Hex2int64(hexStr string) int64

func HexStr2byte

func HexStr2byte(hexStr string) []byte

func HexStr2float32

func HexStr2float32(hexStr string) float32

func HexStr_and

func HexStr_and(hexStr1 string, hexStr2 string) string

func HttpDo

func HttpDo(method string, uri string, header http.Header, data string, params string) string

func Int162hex

func Int162hex(i int64, prefix bool) string

func Int2hex

func Int2hex(i int, prefix bool) string

func Int322hex

func Int322hex(i int64, prefix bool) string

func Int642hex

func Int642hex(i int64, prefix bool) string

func IntToBytes

func IntToBytes(n int64) []byte

用于UTC时间戳转换

func Oct

func Oct(i int, prefix bool) string

func Oct2int

func Oct2int(octStr string) int

func PKCS5Padding

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

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func PKCS7UnPadding

func PKCS7UnPadding(dst []byte, blockSize int) []byte

PKCS7UnPadding pkcs7填充方式

func Str2hex

func Str2hex(str string, length int) (hexStr string)

func SubStr

func SubStr(str string, start, length int) string

func To_md5

func To_md5(encode string) (decode string)

func Token_auth

func Token_auth(signedToken, secret string) (string, error)

Types

type Claims

type Claims struct {
	Appid string `json:"appid"`
	// recommended having
	jwt.StandardClaims
}

type ControllerError

type ControllerError struct {
	Status   int    `json:"status"`
	Code     int    `json:"code"`
	Type     string `json:"type"`
	Message  string `json:"message"`
	MoreInfo string `json:"more_info"`
}

type ControllerSuccess

type ControllerSuccess struct {
	Status int         `json:"status"`
	Data   interface{} `json:"data"`
}

type Hash

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

Hash represents the partial evaluation of a checksum using table-driven implementation. It also implements hash.Hash interface.

func NewHash

func NewHash(crcParams *Parameters) *Hash

NewHash creates a new Hash instance configured for table driven CRC calculation according to parameters specified.

func (*Hash) BlockSize

func (h *Hash) BlockSize() int

BlockSize returns the hash's underlying block size. The Write method must be able to accept any amount of data, but it may operate more efficiently if all writes are a multiple of the block size. See hash.Hash interface.

func (*Hash) CRC

func (h *Hash) CRC() uint64

CRC returns current CRC value for the data processed so far.

func (*Hash) CRC16

func (h *Hash) CRC16() uint16

CRC16 is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls CRC() method.

func (*Hash) CRC32

func (h *Hash) CRC32() uint32

CRC32 is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls CRC() method.

func (*Hash) CalculateCRC

func (h *Hash) CalculateCRC(data []byte) uint64

CalculateCRC is a convenience function allowing to calculate CRC in one call.

func (*Hash) GetTable

func (h *Hash) GetTable() []uint64

func (*Hash) Reset

func (h *Hash) Reset()

Reset resets the Hash to its initial state. See hash.Hash interface.

func (*Hash) Size

func (h *Hash) Size() int

Size returns the number of bytes Sum will return. See hash.Hash interface.

func (*Hash) Sum

func (h *Hash) Sum(in []byte) []byte

Sum appends the current hash to b and returns the resulting slice. It does not change the underlying hash state. See hash.Hash interface.

func (*Hash) Update

func (h *Hash) Update(p []byte)

Update updates process supplied bytes and updates current (partial) CRC accordingly.

func (*Hash) Write

func (h *Hash) Write(p []byte) (n int, err error)

Write implements io.Writer interface which is part of hash.Hash interface.

type Parameters

type Parameters struct {
	Width      uint   // Width of the CRC expressed in bits
	Polynomial uint64 // Polynomial used in this CRC calculation
	ReflectIn  bool   // ReflectIn indicates whether input bytes should be reflected
	ReflectOut bool   // ReflectOut indicates whether input bytes should be reflected
	Init       uint64 // Init is initial value for CRC calculation
	FinalXor   uint64 // Xor is a value for final xor to be applied before returning result
}

Parameters represents set of parameters defining a particular CRC algorithm.

Jump to

Keyboard shortcuts

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