Code Monkey home page Code Monkey logo

xtool's Introduction

xtool

go 常用工具封装

crypto/xaes 简化go aes算法调用

package main

import (
	"fmt"

	"github.com/zh-five/xtool/crypto/xaes"
)

func main() {
	text := "xaes 加密解密示例"
	key := "23rgfdewa" // 任意长度。若长度不等于AES算法的位数,会使用pbkdf2算法格式化对应长度

	xa := xaes.NewAES() // 等价于:xa := initXAES()

	// 加密
	b, _ := xa.Encrypt([]byte(key), []byte(text))

	// 解密
	b2, _ := xa.Decrypt([]byte(key), b)
	text2 := string(b2)

	fmt.Printf("text : %s\ntext1: %s\n", text, text2)
}

func initXAES() *xaes.XAES {
	return xaes.NewAES(
		xaes.SetAES256(),                       // AES位数。另外还有 xaes.SetAES192(), xaes.SetAES128()
		xaes.SetPaddinger(&xaes.PKCS7Pading{}), // 填充算法。另外还有 SetPaddinger(&xaes.ZeroPading{}) 或自定义
		xaes.SetCiphertextCoder(nil),           // 密文编码器。另外还有 xaes.SetCiphertextCoder(&xaes.CiphertextBase64{}) 或自定义

		// 设置iv,任意长度,长度不等于 aes.BlockSize 时,会格式化为 aes.BlockSize
		// 若不设置iv, 加密时会随机生成iv,并把iv附在密文之后;解密时从密文末尾截取iv
		// xaes.SetIv([]byte("adsfasda")),
	)
}

xtool's People

Contributors

zh-five avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.