Skip to content

things-go/encrypt

Repository files navigation

encrypt

加密流

GoDoc Go.Dev reference codecov Action Status Go Report Card License Tag

Installation

    go get github.com/things-go/encrypt

Import:

    import "github.com/things-go/encrypt"

Example

package main

import (
	"bytes"
	"crypto/aes"
	"fmt"

	"github.com/things-go/encrypt"
)

func main() {
	key := []byte("12e41090cd8011ebbe031717db2895df")
	plainText := []byte("im plantext plantext")

	blk, err := encrypt.NewBlockCipher(key, aes.NewCipher)
	if err != nil {
		panic(err)
	}

	cipherText, err := blk.Encrypt(plainText)
	if err != nil {
		panic(err)
	}
	got, err := blk.Decrypt(cipherText)
	if err != nil {
		panic(err)
	}
	if bytes.Equal(plainText, got) {
		fmt.Println("encrypt success")
	} else {
		panic("not equal")
	}
}
package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"fmt"

	"github.com/things-go/encrypt"
)

func main() {
	key := []byte("0123456789123456")
	plainText := []byte("im plantext")

	blk, err := encrypt.NewStreamCipher(key, aes.NewCipher, encrypt.WithStreamCodec(cipher.NewCTR, cipher.NewCTR))
	if err != nil {
		panic(err)
	}

	cipherText, err := blk.Encrypt(plainText)
	if err != nil {
		panic(err)
	}
	got, err := blk.Decrypt(cipherText)
	if err != nil {
		panic(err)
	}
	if bytes.Equal(plainText, got) {
		fmt.Println("encrypt success")
	} else {
		panic("not equal")
	}
}
package main

import (
	"bytes"
	"fmt"

	"github.com/things-go/encrypt"
)

func main() {
	password := "pass_word"
	plainText := []byte("hello world")

	cip, err := encrypt.NewCipher("aes-128-cfb", password)
	if err != nil {
		panic(err)
	}
	// encrypt
	cipherText := make([]byte, len(plainText))
	cip.Write.XORKeyStream(cipherText, plainText)
	// decrypt
	got := make([]byte, len(cipherText))
	cip.Read.XORKeyStream(got, cipherText)

	if bytes.Equal(got, plainText) {
		fmt.Println("encrypt success")
	} else {
		panic("not equal")
	}
}

License

This project is under MIT License. See the LICENSE file for the full license text.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages