tools

package
v0.0.0-...-3534724 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// LowerLetters is the list of lowercase letters.
	LowerLetters = "abcdefghijklmnopqrstuvwxyz"

	// UpperLetters is the list of uppercase letters.
	UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// Digits is the list of permitted digits.
	Digits = "0123456789"

	// Symbols is the list of symbols.
	Symbols = "~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
)

Variables

View Source
var (
	// ErrExceedsTotalLength is the error returned with the number of digits and
	// symbols is greater than the total length.
	ErrExceedsTotalLength = errors.New("number of digits and symbols must be less than total length")

	// ErrLettersExceedsAvailable is the error returned with the number of letters
	// exceeds the number of available letters and repeats are not allowed.
	ErrLettersExceedsAvailable = errors.New("number of letters exceeds available letters and repeats are not allowed")

	// ErrDigitsExceedsAvailable is the error returned with the number of digits
	// exceeds the number of available digits and repeats are not allowed.
	ErrDigitsExceedsAvailable = errors.New("number of digits exceeds available digits and repeats are not allowed")

	// ErrSymbolsExceedsAvailable is the error returned with the number of symbols
	// exceeds the number of available symbols and repeats are not allowed.
	ErrSymbolsExceedsAvailable = errors.New("number of symbols exceeds available symbols and repeats are not allowed")
)

Functions

func Contains

func Contains(array interface{}, val interface{}) bool

Contains 判断一个元素是否存在与某一个数组中

func DecryptAES

func DecryptAES(ciphertext []byte, key string) ([]byte, error)

解密

func EncryptAES

func EncryptAES(plaintext []byte, key string) ([]byte, error)

加密

func Generate

func Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error)

Generate is the package shortcut for Generator.Generate.

Example
res, err := Generate(64, 10, 10, false, false)
if err != nil {
	log.Fatal(err)
}
log.Print(res)
Output:

func HamSha1

func HamSha1(data string, key []byte) []byte

func MD5

func MD5(byteMessage []byte) string

对数据进行md5计算

func MustGenerate

func MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string

MustGenerate is the package shortcut for Generator.MustGenerate.

Example
// Will panic on error
res := MustGenerate(64, 10, 10, false, false)
log.Print(res)
Output:

func PKCS7Pad

func PKCS7Pad(data []byte) []byte

Padding补全

func PKCS7UPad

func PKCS7UPad(data []byte) []byte

func PanicDetail

func PanicDetail() string

func Randpw

func Randpw(length int) string

func Sha1

func Sha1(data string) string

对字符串进行sha1 计算

func TimeoutedExec

func TimeoutedExec(timeout time.Duration, name string, arg ...string) (string, error)

TimeoutedExec executes a timeouted command. The program path is defined by the name arguments, args are passed as arguments to the program.

TimeoutedExec returns process output as a string (stdout) , and stderr as an error.

Types

type Claims

type Claims struct {
	jwt.StandardClaims
	UserUUID string `json:"uuid"`
}

type Credential

type Credential struct {
	User  *models.User
	Roles []*models.Role
}

type Generator

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

Generator is the stateful generator which can be used to customize the list of letters, digits, and/or symbols.

func NewGenerator

func NewGenerator() (*Generator, error)

NewGenerator creates a new Generator from the specified configuration. If no input is given, all the default values are used. This function is safe for concurrent use.

Example (Custom)
// Customize the list of symbols.
gen, err := NewGenerator(&GeneratorInput{
	Symbols: "!@#$%^()",
})
if err != nil {
	log.Fatal(err)
}

_ = gen // gen.Generate(...)
Output:

Example (Nil)
// This is exactly the same as calling "Generate" directly. It will use all
// the default values.
gen, err := NewGenerator(nil)
if err != nil {
	log.Fatal(err)
}

_ = gen // gen.Generate(...)
Output:

func (*Generator) Generate

func (g *Generator) Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error)

Generate generates a password with the given requirements. length is the total number of characters in the password. numDigits is the number of digits to include in the result. numSymbols is the number of symbols to include in the result. noUpper excludes uppercase letters from the results. allowRepeat allows characters to repeat.

The algorithm is fast, but it's not designed to be performant; it favors entropy over speed. This function is safe for concurrent use.

Example
gen, err := NewGenerator(nil)
if err != nil {
	log.Fatal(err)
}

res, err := gen.Generate(64, 10, 10, false, false)
if err != nil {
	log.Fatal(err)
}
log.Print(res)
Output:

func (*Generator) MustGenerate

func (g *Generator) MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string

MustGenerate is the same as Generate, but panics on error.

type JWT

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

TODO: 结构体名称不太好

func New

func New() *JWT

TODO: New这个函数名称不好,容易造成误解

func (*JWT) CreateToken

func (this *JWT) CreateToken(user *models.User) (token string, err error)

func (*JWT) Delete

func (this *JWT) Delete(key string)

func (*JWT) FromContext

func (this *JWT) FromContext(ctx context.Context) *Credential

func (*JWT) Load

func (this *JWT) Load(key string) (string, bool)

func (*JWT) ParseToken

func (this *JWT) ParseToken(input string) (cred Credential, err error)

ParseToken 解析令牌,生成凭证

func (*JWT) Store

func (this *JWT) Store(key string, value string)

type MailDto

type MailDto struct {
	Subject string
	Body    string
}

TODO: 读取配置,代码重构

func (*MailDto) Send

func (this *MailDto) Send() (bool, error)

Jump to

Keyboard shortcuts

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