cryptos

package
v1.0.49 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cryptos 密码.

文本加密/解密,支持AES、DES,多用于配置文件密码加密.

密文格式: 加密方法(base64)

AES(NR501ySw4TYFc0dI8zERfA==)
DES(2lz8gGSpYbA=)

AES安全性较高,DES密文较短.

Example
package main

import (
	"fmt"

	"github.com/xuender/oils/cryptos"
)

func main() {
	password := "password"
	txt := cryptos.Encrypt("123", password, cryptos.DES)

	// fmt.Println(txt)
	fmt.Println(cryptos.IsEncrypt(txt))
	fmt.Println(cryptos.Decrypt(txt, password))

}
Output:

true
123 <nil>

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnpaddingLength = errors.New("invalid unpadding length")
	ErrNoEncrypt       = errors.New("no encrypt")
)

Functions

func Decrypt

func Decrypt(src, key string) (string, error)

Decrypt 解密.

str, _ := cryptos.Decrypt("AES(NR501ySw4TYFc0dI8zERfA==)", "password")
fmt.Println(str)
// 123

func Encrypt

func Encrypt(str, key string, cipher Cipher) string

Encrypt 加密.

txt := cryptos.Encrypt("123", "password", cryptos.AES)
fmt.Println(txt)
// AES(NR501ySw4TYFc0dI8zERfA==)
Example
package main

import (
	"fmt"

	"github.com/xuender/oils/cryptos"
)

func main() {
	key := "key"
	str := cryptos.Encrypt("aaa", key, cryptos.AES)
	fmt.Println(cryptos.Decrypt(str, key))

	str = cryptos.Encrypt("AAA", key, cryptos.DES)
	fmt.Println(cryptos.Decrypt(str, key))

}
Output:

aaa <nil>
AAA <nil>

func IsEncrypt

func IsEncrypt(str string) bool

IsEncrypt 是否加密.

func Padding

func Padding(str string, blockSize int) []byte

func UnPadding

func UnPadding(cipherText []byte) (string, error)

Types

type Cipher

type Cipher int
const (
	AES Cipher = iota
	DES
)

func Parse

func Parse(str string) (src []byte, cipher Cipher, err error)

Parse 解析密文, 返回数据和加密算法.

func (Cipher) Block

func (p Cipher) Block(key string) cipher.Block

func (Cipher) String

func (p Cipher) String() string

func (Cipher) Stringify

func (p Cipher) Stringify(data []byte) string

Jump to

Keyboard shortcuts

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