41 lines
743 B
Go
41 lines
743 B
Go
package tscipher
|
|
|
|
import (
|
|
chacha "github.com/codahale/chacha20"
|
|
"log"
|
|
)
|
|
|
|
type ChaCha struct {
|
|
key []byte
|
|
nonce []byte
|
|
}
|
|
|
|
func (this *ChaCha) Decrypt(data []byte) (decrypted []byte, err error) {
|
|
// xor, err := chacha.NewXChaCha(this.key, this.nonce)
|
|
if err != nil {
|
|
log.Fatalln("Decrypt", err)
|
|
}
|
|
// xor.XORKeyStream(data, data)
|
|
decrypted = data
|
|
err = nil
|
|
return
|
|
}
|
|
|
|
func (this *ChaCha) Encrypt(data []byte) (encryped []byte, err error) {
|
|
xor, err := chacha.NewXChaCha(this.key, this.nonce)
|
|
if err != nil {
|
|
log.Fatalln("Chacha Encrypt", err)
|
|
}
|
|
xor.XORKeyStream(data, data)
|
|
encryped = data
|
|
err = nil
|
|
return
|
|
}
|
|
|
|
func NewChaCha() (cipher Cipher) {
|
|
return &ChaCha{
|
|
key: make([]byte, 256),
|
|
nonce: make([]byte, 64),
|
|
}
|
|
}
|