gone

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 25 Imported by: 4

README

gone

gone是一个简单、方便、小巧好用工具包.

非对称加密解密与签名

// GenerateBase64Key 生成base64编码的公私钥
func GenerateBase64Key(secretLength SecretKeyLengthType, secretFormat SecretKeyFormatType) (pkStr string, pbkStr string, err error)

// SignBySM2Bytes 使用sm2私钥签名字符串,返回base64编码的license
func SignBySM2Bytes(privateKey, licenseBytes []byte) (license string, err error) 

// SignBySM2  使用sm2私钥对象指针签名字符串,返回base64编码的license
func SignBySM2(privateKey *sm2.PrivateKey, licenseBytes []byte) (license string, err error)

// SignByRSABytes 使用rsa私钥签名字符串,返回base64编码的license
func SignByRSABytes(key, licenseBytes []byte) (string, error) 

// SignByRSA 使用rsa私钥对象指针签名字符串,返回base64编码的license
func SignByRSA(key *rsa.PrivateKey, licenseBytes []byte) (license string, err error)

// VerifyBySM2 使用sm2公钥验证签名的license
func VerifyBySM2(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

// VerifyByRSA 使用rsa公钥验证签名的license
func VerifyByRSA(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

// RsaPublicEncrypt Rsa公钥加密,参数publicKeyStr必须是hex、base64或者是pem编码
func RsaPublicEncrypt(publicKeyStr string, textBytes []byte) ([]byte, error) 

// ParsePublicKey 解析公钥,derBytes可以使用DecodePemHexBase64函数获取
func ParsePublicKey(derBytes []byte) (publicKey *rsa.PublicKey, err error)

// RsaPrivateDecrypt 解析rsa私钥,参数privateKeyStr必须是hex、base64或者是pem编码
func RsaPrivateDecrypt(privateKeyStr string, cipherBytes []byte) (textBytes []byte, err error)

// ParsePrivateKey 解析私钥,derBytes可以使用DecodePemHexBase64函数获取
func ParsePrivateKey(derBytes []byte) (privateKey *rsa.PrivateKey, err error) 

// DecodePemHexBase64 解析pem或者hex或者base64编码成der编码
func DecodePemHexBase64(keyStr string) ([]byte, error)

随机函数

// RandInt64 指定范围内的随机数字,max必须大于min。
func RandInt64(min int64, max int64) int64

// RandInt32 指定范围内的随机数字,max必须大于min。
func RandInt32(min int32, max int32) int32 

// RandInt 指定范围内的随机数字
func RandInt(min int, max int) int 

// RandInts 生成指定范围int类型数组
func RandInts(from, to, size int) []int

// RandLower 指定长度的随机小写字母
func RandLower(l int) string

// RandUpper 指定长度的随机大写字母
func RandUpper(l int) string


// RandBytes 生成随机长度字节
func RandBytes(length int) []byte 

// RandString 生成随机长度字符串,推荐使用。
func RandString(n int) string 

文件处理函数

// FileSize 获取文件大小
func FileSize(path string) 

// FileSize 判断文件是否存在
func FileExist(file string) bool

// FileIsBinary 判断文件是否是二进制文件
func FileIsBinary(content string) bool

// FileIsDir 判断文件是否是目录
func FileIsDir(path string) bool

// FileFindAPath 获取文件名路径,首先判断文件是否可以直接访问,优先获取当前可执行文件夹下,再去找工作路径下。
func FileFindPath(fname string)

aes加解密函数

// AesEncryptCBC 加密 AES-128。key长度:16, 24, 32 bytes 对应 AES-128, AES-192, AES-256
func AesEncryptCBC(origData, key, iv []byte) (encrypted []byte, err error)

// AesDecryptCBC cbc模式解密
func AesDecryptCBC(encrypted []byte, key, iv []byte) (decrypted []byte, err error)

字符串转换函数

// BytesToStr []byte转string
func BytesToStr(b []byte) string

// StrToBytes string转[]byte
func StrToBytes(str string) []byte

// Contains 判断字符串是否存在切片中
func Contains(str string, s []string) bool

// StringReplaceIgnoreCase 忽略大小写替换字符串
func StringReplaceIgnoreCase(text, source, target string) string

时间格式化函数

// TimeToStr 返回时间的字符串格式
func TimeToStr(t time.Time, format ...string) string 

// Timestamp将unix时间转为时间字符串
func TimestampToStr(t int64, format ...string) string

// FormatByStr 将字符串中的时间变量(y年/m月/d日/h时/i分/s秒)转换成时间字符串
func FormatByStr(tpl string, t int64) string 

// GetMonthRange 获得指定年份和月份的起始unix时间和截止unix时间
func GetMonthRange(year int, month int) (beginTime, endTime int64, err error) 

// GetWeek 获得星期的数字
func GetWeek(t time.Time) int

其他函数

//MacAddr 获取机器mac地址,返回mac字串数组
func MacAddr() (upMac []string, err error)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AesDecryptCBC

func AesDecryptCBC(encrypted []byte, key, iv []byte) (decrypted []byte, err error)

AesDecryptCBC cbc模式解密

func AesEncryptCBC

func AesEncryptCBC(origData, key, iv []byte) (encrypted []byte, err error)

AesEncryptCBC 加密 AES-128。key长度:16, 24, 32 bytes 对应 AES-128, AES-192, AES-256

func BytesToStr

func BytesToStr(b []byte) string

BytesToStr []byte转string

func Contains

func Contains(str string, s []string) bool

Contains 判断字符串是否存在切片中

func DecodePemHexBase64

func DecodePemHexBase64(keyStr string) ([]byte, error)

DecodePemHexBase64 解析pem或者hex或者base64编码成der编码

func FileCopy

func FileCopy(source string, dest string) (err error)

FileCopy 复制文件,代码仅供参考 Deprecated

func FileExist

func FileExist(file string) bool

FileSize 判断文件是否存在

func FileFindPath

func FileFindPath(fname string) (string, error)

FileFindAPath 获取文件名路径,首先判断文件是否可以直接访问,优先获取当前可执行文件夹下,再去找工作路径下。

func FileIsBinary

func FileIsBinary(content string) bool

FileIsBinary 判断文件是否是二进制文件

func FileIsDir

func FileIsDir(path string) bool

FileIsDir 判断文件是否是目录

func FileMove

func FileMove(src, dest string) error

FileMove 移动文件,代码仅供参考 Deprecated

func FileSize

func FileSize(path string) int64

FileSize 获取文件大小

func FormatByStr

func FormatByStr(tpl string, t int64) string

FormatByStr 将字符串中的时间变量(y年/m月/d日/h时/i分/s秒)转换成时间字符串

func GenerateBase64Key

func GenerateBase64Key(secretLength SecretKeyLengthType, secretFormat SecretKeyFormatType) (pkStr string, pbkStr string, err error)

GenerateBase64Key 生成base64编码的公私钥

func GetMonthRange

func GetMonthRange(year int, month int) (beginTime, endTime int64, err error)

GetMonthRange 获得指定年份和月份的起始unix时间和截止unix时间

func GetWeek

func GetWeek(t time.Time) int

GetWeek 获得星期的数字

func MacAddr

func MacAddr() (upMac []string, err error)

MacAddr 获取机器mac地址,返回mac字串数组

func ParsePrivateKey

func ParsePrivateKey(derBytes []byte) (privateKey *rsa.PrivateKey, err error)

ParsePrivateKey 解析私钥,derBytes可以使用DecodePemHexBase64函数获取

func ParsePublicKey

func ParsePublicKey(derBytes []byte) (publicKey *rsa.PublicKey, err error)

ParsePublicKey 解析公钥,derBytes可以使用DecodePemHexBase64函数获取

func RandAlphaString

func RandAlphaString(length int) string

RandAlphaString 生成随机长度字母 Deprecated

func RandBytes

func RandBytes(length int) []byte

RandBytes 生成随机长度字节

func RandInt

func RandInt(min int, max int) int

RandInt 指定范围内的随机数字

func RandInt32

func RandInt32(min int32, max int32) int32

RandInt32 指定范围内的随机数字,max必须大于min。

func RandInt64

func RandInt64(min int64, max int64) int64

RandInt64 指定范围内的随机数字,max必须大于min。

func RandInts

func RandInts(from, to, size int) []int

RandInts 生成指定范围int类型数组

func RandLower

func RandLower(l int) string

RandLower 指定长度的随机小写字母

func RandString

func RandString(n int) string

RandString 生成随机长度字符串,推荐使用。

func RandUpper

func RandUpper(l int) string

RandUpper 指定长度的随机大写字母

func RsaPrivateDecrypt

func RsaPrivateDecrypt(privateKeyStr string, cipherBytes []byte) (textBytes []byte, err error)

RsaPrivateDecrypt 解析rsa私钥,参数privateKeyStr必须是hex、base64或者是pem编码

func RsaPublicEncrypt

func RsaPublicEncrypt(publicKeyStr string, textBytes []byte) ([]byte, error)

RsaPublicEncrypt Rsa公钥加密,参数publicKeyStr必须是hex、base64或者是pem编码

func SignByRSA

func SignByRSA(key *rsa.PrivateKey, licenseBytes []byte) (license string, err error)

SignByRSA 使用rsa私钥对象指针签名字符串,返回base64编码的license

func SignByRSABytes

func SignByRSABytes(key, licenseBytes []byte) (string, error)

SignByRSABytes 使用rsa私钥签名字符串,返回base64编码的license

func SignBySM2

func SignBySM2(privateKey *sm2.PrivateKey, licenseBytes []byte) (license string, err error)

SignBySM2 使用sm2私钥对象指针签名字符串,返回base64编码的license

func SignBySM2Bytes

func SignBySM2Bytes(privateKey, licenseBytes []byte) (license string, err error)

SignBySM2Bytes 使用sm2私钥签名字符串,返回base64编码的license

func StrToBytes

func StrToBytes(str string) []byte

StrToBytes string转[]byte

func StringReplaceIgnoreCase

func StringReplaceIgnoreCase(text, source, target string) string

StringReplaceIgnoreCase 忽略大小写替换字符串

func TimeToStr

func TimeToStr(t time.Time, format ...string) string

TimeToStr 返回时间的字符串格式

func TimestampToStr

func TimestampToStr(t int64, format ...string) string

Timestamp将unix时间转为时间字符串

func VerifyByRSA

func VerifyByRSA(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

VerifyByRSA 使用rsa公钥验证签名的license

func VerifyBySM2

func VerifyBySM2(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

VerifyBySM2 使用sm2公钥验证签名的license

Types

type SecretKeyFormatType

type SecretKeyFormatType int
const (
	PKCS8 SecretKeyFormatType = 1
	PKCS1 SecretKeyFormatType = 2
)

func SecretKeyFormatTypeParse

func SecretKeyFormatTypeParse(v int) SecretKeyFormatType

type SecretKeyLengthType

type SecretKeyLengthType int
const (
	RSA SecretKeyLengthType = 1
	M2  SecretKeyLengthType = 2
)

func SecretKeyLengthTypeParse

func SecretKeyLengthTypeParse(v int) SecretKeyLengthType

Jump to

Keyboard shortcuts

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