utils

package
v1.10.6 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: GPL-3.0 Imports: 40 Imported by: 8

Documentation

Index

Constants

View Source
const (
	ExtZip = ".zip"
)

Variables

View Source
var (
	JSONMarshal       = json.Marshal
	JSONUnmarshal     = json.Unmarshal
	JSONMarshalIndent = json.MarshalIndent
	JSONNewDecoder    = json.NewDecoder
	JSONNewEncoder    = json.NewEncoder
)

定义JSON操作

View Source
var DefaultTxtKey = "Ifmkbook1qaz*WSP"

16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法 key不能泄露 var PwdKey = []byte("DIS**#KKKDJJSKDI")

View Source
var DisableCache bool

DisableCache will disable caching of the home directory. Caching is enabled by default.

Functions

func Abs

func Abs(x int) int

func AesDecrypt added in v1.10.2

func AesDecrypt(cypted []byte, key []byte) (string, error)

AesDecrypt 实现解密

func AesDemo added in v1.10.2

func AesDemo()

func AesDemo2 added in v1.10.2

func AesDemo2()

func AesEncrypt added in v1.10.2

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

AesEncrypt 实现加密

func AnyToAny

func AnyToAny(src interface{}, des interface{}) error

func Base64Dec

func Base64Dec(encodeString string) string

Base64Dec base64解码

func Base64DecUrl

func Base64DecUrl(encodeString string) string

Base64DecUrl base64 URL解码

func Base64Enc

func Base64Enc(input []byte) string

Base64Enc base64编码

func Base64EncUrl

func Base64EncUrl(input []byte) string

Base64EncUrl base64 URL编码

func Black

func Black(str string, modifier ...interface{}) string

黑色

func Blue

func Blue(str string, modifier ...interface{}) string

蓝色

func Brown

func Brown(str string, modifier ...interface{}) string

棕色

func CertInfo added in v1.0.8

func CertInfo(certPath string, certBytes []byte) (*x509.Certificate, error)

CertInfo "test.pem"

func CheckEmail

func CheckEmail(email string) error

CheckEmail will check a email is valid

func Compress added in v1.0.8

func Compress(srcDir, dstZipPath string) error

func CurrentUTCTime

func CurrentUTCTime() string

func Cyan

func Cyan(str string, modifier ...interface{}) string

青色/蓝绿色

func DarkGray

func DarkGray(str string, modifier ...interface{}) string

深灰色

func DeTxtByAes added in v1.10.2

func DeTxtByAes(pwd, PwdKey string) string

DeTxtByAes 解密base64 aes

func DecBase64ByAes added in v1.10.2

func DecBase64ByAes(data string, keyText string) []byte

DecBase64ByAes Aes + base64 解密

func Decrypt3DES

func Decrypt3DES(src, private []byte) []byte

Decrypt3DES 使用des进行解密

func DecryptDES

func DecryptDES(src, private []byte) []byte

DecryptDES 使用des进行解密

func Difference

func Difference(slice1, slice2 []string) []string

求差集 slice1-并集

func Dir

func Dir() (string, error)

Dir returns the home directory for the executing user.

This uses an OS-specific method for discovering the home directory. An error is returned if a home directory cannot be detected.

func EnTxtByAes added in v1.10.2

func EnTxtByAes(pwdStr, PwdKey string) string

EnTxtByAes 加密base64

func EncBase64ByAes added in v1.10.2

func EncBase64ByAes(data string, keyText string) string

EncBase64ByAes Aes加密 后 base64 再加密

func Encrypt3DES

func Encrypt3DES(src []byte, private []byte) []byte

Encrypt3DES 使用des进行对称加密 length 24

func EncryptDES

func EncryptDES(src []byte, private []byte) []byte

EncryptDES 使用des进行对称加密 length 8

func EnsureDirExist

func EnsureDirExist(name string) error

func Expand

func Expand(path string) (string, error)

Expand expands the path to include the home directory if the path is prefixed with `~`. If it isn't prefixed with `~`, the path is returned as-is.

func FileExists

func FileExists(name string) bool

func GetMapDefault

func GetMapDefault(k, defaultValue interface{}, m map[interface{}]interface{}) interface{}

func GetRandomString

func GetRandomString(length int) string

func GetSrcZips added in v1.0.8

func GetSrcZips(src string) ([]string, error)

func Green

func Green(str string, modifier ...interface{}) string

绿色字体,modifier里,第一个控制闪烁,第二个控制下划线

func GzipCompressFile

func GzipCompressFile(srcPath, dstPath string) error

func HashAndSalt added in v1.0.8

func HashAndSalt(pwd []byte) string

HashAndSalt Hash & Salt 用户的密码

func In

func In(haystack interface{}, needle interface{}) (bool, error)

In example: r, err := In([]interface{}{1, "two", 3}, "two")

func InIntSliceMapKeyFunc

func InIntSliceMapKeyFunc(haystack []int) func(int) bool

func InOfInt

func InOfInt(obj int, arrayList []int) bool

func InOfInt32

func InOfInt32(obj int32, arrayList []int32) bool

func InOfInt64

func InOfInt64(obj int64, arrayList []int64) bool

func InOfStr

func InOfStr(str string, arrayList []string) bool

func InOfType

func InOfType(obj interface{}, arrayList []interface{}, dataType string) bool

func Intersect

func Intersect(slice1, slice2 []string) []string

求交集

func IsDir added in v1.0.8

func IsDir(src string) (bool, error)

func IsFileExist added in v1.0.8

func IsFileExist(filePath string) (bool, error)

func IsZip added in v1.0.8

func IsZip(src string) (bool, error)

func JSONMarshalToString

func JSONMarshalToString(v interface{}) string

JSONMarshalToString JSON编码为字符串

func LightBlue

func LightBlue(str string, modifier ...interface{}) string

淡蓝

func LightCyan

func LightCyan(str string, modifier ...interface{}) string

淡青色

func LightGray

func LightGray(str string, modifier ...interface{}) string

浅灰色

func LightGreen

func LightGreen(str string, modifier ...interface{}) string

淡绿

func LightPurple

func LightPurple(str string, modifier ...interface{}) string

淡紫色

func LightRed

func LightRed(str string, modifier ...interface{}) string

淡红色

func MD5Hash

func MD5Hash(b []byte) string

MD5Hash MD5哈希值

func MD5HashString

func MD5HashString(s string) string

MD5HashString MD5哈希值 返回一个32位md5加密后的字符串

func MD5HashString16

func MD5HashString16(data string) string

MD5HashString16 返回一个16位md5加密后的字符串

func Marshal

func Marshal(v interface{}) (string, error)

Marshal ToJSON 转换为JSON Json Marshal:将数据编码成json字符串

func PKCS7Padding added in v1.10.2

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

PKCS7Padding PKCS7 填充模式 填充最后一个分组的函数 ciphertext - 原始数据 blockSize - 每个分组的数据长度

func PKCS7UnPadding added in v1.10.2

func PKCS7UnPadding(origData []byte) ([]byte, error)

PKCS7UnPadding 填充的反向操作,删除填充字符串

func Pop

func Pop(haystack interface{}, needle interface{}) (bool, error)

func PrettyJson

func PrettyJson(data interface{}) (string, error)

func Purple

func Purple(str string, modifier ...interface{}) string

紫色

func RandString

func RandString(len int) string

func Red

func Red(str string, modifier ...interface{}) string

红字体

func Request added in v1.0.8

func Request(url, method string, param interface{}, headers []map[string]string, out interface{}) ([]byte, error)

func Reset

func Reset()

Reset clears the cache, forcing the next call to Dir to re-detect the home directory. This generally never has to be called, but can be useful in tests if you're modifying the home directory via the HOME env var or something.

func SHA1Hash

func SHA1Hash(b []byte) string

SHA1Hash SHA1哈希值

func SHA1HashString

func SHA1HashString(s string) string

SHA1HashString SHA1哈希值

func SHA256Hash

func SHA256Hash(b []byte) string

SHA256Hash SHA256哈希值

func SHA256HashString

func SHA256HashString(s string) string

SHA256HashString SHA1哈希值

func SortInIntSlice

func SortInIntSlice(haystack []int, needle int) bool

二分查找

func Sum

func Sum(i []int) int

func TestAll

func TestAll()

func UnCompress added in v1.0.8

func UnCompress(src, dst string) error

func Union

func Union(slice1, slice2 []string) []string

求并集

func Unmarshal

func Unmarshal(s string, v interface{}) error

Unmarshal ToStruct 转换为Struct Json Unmarshal:将json字符串解码到相应的数据结构

func UrlEncode added in v1.0.8

func UrlEncode(param map[string]string) string

func ValidateSaltPasswords added in v1.0.8

func ValidateSaltPasswords(hashedPwd string, plainPwd []byte) bool

ValidateSaltPasswords 验证密码 参数 hashedPwd: Hash & Salt 用户的密码,plainPwd: 明文密码

func White

func White(str string, modifier ...interface{}) string

白色

func Yellow

func Yellow(str string, modifier ...interface{}) string

黄色字体

Types

type Hash

type Hash struct{}

func (*Hash) MD5HashString

func (h *Hash) MD5HashString(s string) string

MD5HashString MD5哈希值

func (*Hash) SHA1HashString

func (h *Hash) SHA1HashString(s string) string

func (*Hash) SHA256HashString

func (h *Hash) SHA256HashString(s string) string

type IHash

type IHash interface {
	MD5HashString(s string) string
	SHA1HashString(s string) string
	SHA256HashString(s string) string
}

type ResponseExec added in v1.0.8

type ResponseExec struct {
	ExitCode int    `json:"exit_code"`
	Err      error  `json:"err"`
	Stdout   string `json:"stdout"`
}

type S

type S string

S 字符串类型转换

func (S) Bool

func (s S) Bool() (bool, error)

Bool 转换为bool

func (S) Bytes

func (s S) Bytes() []byte

Bytes 转换为[]byte

func (S) DefaultBool

func (s S) DefaultBool(defaultVal bool) bool

DefaultBool 转换为bool,如果出现错误则使用默认值

func (S) DefaultFloat32

func (s S) DefaultFloat32(defaultVal float32) float32

DefaultFloat32 转换为float32,如果出现错误则使用默认值

func (S) DefaultFloat64

func (s S) DefaultFloat64(defaultVal float64) float64

DefaultFloat64 转换为float64,如果出现错误则使用默认值

func (S) DefaultInt

func (s S) DefaultInt(defaultVal int) int

DefaultInt 转换为int,如果出现错误则使用默认值

func (S) DefaultInt64

func (s S) DefaultInt64(defaultVal int64) int64

DefaultInt64 转换为int64,如果出现错误则使用默认值

func (S) DefaultUint

func (s S) DefaultUint(defaultVal int) int

DefaultUint 转换为uint,如果出现错误则使用默认值

func (S) DefaultUint64

func (s S) DefaultUint64(defaultVal uint64) uint64

DefaultUint64 转换为uint64,如果出现错误则使用默认值

func (S) Float32

func (s S) Float32() (float32, error)

Float32 转换为float32

func (S) Float64

func (s S) Float64() (float64, error)

Float64 转换为float64

func (S) Int

func (s S) Int() (int, error)

Int 转换为int

func (S) Int64

func (s S) Int64() (int64, error)

Int64 转换为int64

func (S) JsonToString

func (s S) JsonToString(v interface{}) error

func (S) String

func (s S) String() string

func (S) Uint

func (s S) Uint() (int, error)

Uint 转换为uint

func (S) Uint64

func (s S) Uint64() (uint64, error)

Uint64 转换为uint64

type UUID

type UUID [16]byte

A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.

var Nil UUID

Nil empty UUID, all zeros

func Must

func Must(uuid UUID, err error) UUID

Must returns uuid if err is nil and panics otherwise.

func NewHash

func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID

NewHash returns a new UUID derived from the hash of space concatenated with data generated by h. The hash should be at least 16 byte in length. The first 16 bytes of the hash are used to form the UUID. The version of the UUID will be the lower 4 bits of version.

func NewMD5

func NewMD5(space UUID, data []byte) UUID

NewMD5 returns a new MD5 (Version 3) UUID based on the supplied name space and data. It is the same as calling:

NewHash(md5.New(), space, data, 3)

func NewRandom

func NewRandom() (UUID, error)

NewRandom returns a Random (Version 4) UUID.

The strength of the UUIDs is based on the strength of the crypto/rand package.

A note about uniqueness derived from the UUID Wikipedia entry:

Randomly generated UUIDs have 122 random bits.  One's annual risk of being
hit by a meteorite is estimated to be one chance in 17 billion, that
means the probability is about 0.00000000006 (6 × 10−11),
equivalent to the odds of creating a few tens of trillions of UUIDs in a
year and having one duplicate.

func NewSHA1

func NewSHA1(space UUID, data []byte) UUID

NewSHA1 returns a new SHA1 (Version 5) UUID based on the supplied name space and data. It is the same as calling:

NewHash(sha1.New(), space, data, 5)

func (UUID) Bytes

func (uuid UUID) Bytes() []byte

Bytes returns bytes slice representation of UUID.

func (UUID) String

func (uuid UUID) String() string

String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx , or "" if uuid is invalid.

Jump to

Keyboard shortcuts

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