Skip to content

πŸ”– Crypto ​

go
import (
	"github.com/thuongtruong109/gouse"
)

1. Encode data ​

Description: Encode data to base64
Input params: (data []byte)

go
func EncodeData() {
	data := []byte("This is a sample data")

	encodedData, err := gouse.EncodeData(data)
	if err != nil {
		gouse.Println("Error encoding data:", err)
		return
	}
	gouse.Println("Raw data:", string(data))
	gouse.Println("Encoded data:", string(encodedData))
}

2. Decode data ​

Description: Decode data from base64
Input params: (data []byte)

go
func DecodeData() {
	data := []byte("VGhpcyBpcyBhIHNhbXBsZSBkYXRh")
	decodedData, err := gouse.DecodeData(data)
	if err != nil {
		gouse.Println("Error decoding data:", err)
		return
	}
	gouse.Println("Raw encoded data:", string(data))
	gouse.Println("Decoded data:", string(decodedData))
}

3. Encrypt file ​

Description: Encrypt data in file
Input params: (filename string, password []byte)

go
func EncryptFile() {
	gouse.EncryptFile("sample.txt", []byte("password"))
	println("File content encrypted")
}

4. Decrypt file ​

Description: Decrypt data in file
Input params: (filename string, password []byte)

go
func DecryptFile() {
	gouse.DecryptFile("sample.txt", []byte("password"))
	println("File content decrypted")
}

5. Encrypt password ​

Description: Encrypt password string
Input params: (data string)

go
func EncryptPassword() {
	data := "This is a sample data"

	encryptedData, err := gouse.EncryptPassword(data)
	if err != nil {
		gouse.Println("Error encrypting data:", err)
		return
	}
	gouse.Println("Raw data:", string(data))
	gouse.Println("Encrypted data:", string(encryptedData))
}

6. Decrypt password ​

Description: Decrypt password string and compare with the original password
Input params: (data string, password string)

go
func DecryptPassword() {
	data := "$2a$10$bcA002IOHi5SYHNH4lmIbuHjHplGl7TQZ.MznNrL1N70vAi7ovTa2"
	err := gouse.DecryptPassword(data, "This is a sample data")
	if err != nil {
		gouse.Println("Error decrypting data:", err)
		return
	}
	println("Password matched")
}

Released under the MIT License.