encrypt

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EncryptEnv     = `ENCRYPT_ENV`
	EncryptDataVar = `encrypted-data`

	QREnv     = `QR_ENV`
	QRDataVar = `qr-data`

	QRBinEnv     = `QR_BIN_ENV`
	QRBinDataVar = `qr-bin-data`

	EmbeddedImagePathEnv = `EMBEDDED_IMAGE_PATH_ENV`
	EmbeddedImagePathVar = `embedded-image-path`
)

Variables

View Source
var CreateQRBinaryCmd = &bonzai.Cmd{
	Name:  `binary`,
	Alias: `bin`,
	Comp:  comp.Cmds,
	Short: `qrcode related commands`,
	Cmds: []*bonzai.Cmd{
		EmbedCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Vars: bonzai.Vars{
		{
			K: QRBinDataVar,
			V: `foo`,
			E: QRBinEnv,
			S: `binary data representing a qrcode`,
			P: true,
		},
	},
	Long: `
created QRCode then transform it in binary data.

Usages:
- encrypt text <input> <key> qrcode binary embed <input-image>;
`,
	Do: func(x *bonzai.Cmd, args ...string) error {
		fmt.Println("--- binary ---")
		fmt.Println(args)

		switch args[0] {
		case EmbedCmd.Name:
			EmbedCmd.Do(x, args[1:]...)
		}
		return nil
	},
}
View Source
var EmbedCmd = &bonzai.Cmd{
	Name:  `embed`,
	Comp:  comp.Cmds,
	Short: `DCT (Discrete Cosine Transform) embedding`,
	Cmds: []*bonzai.Cmd{
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Vars: bonzai.Vars{
		{
			K: QRBinDataVar,
			V: `foo`,
			E: QRBinEnv,
			S: `binary data representing a QR code`,
			P: true,
		},
		{
			K: EmbeddedImagePathVar,
			V: `/tmp/embedded-image.jpg`,
			E: EmbeddedImagePathEnv,
			S: `path to output: embedded image`,
			P: true,
		},
	},
	Long: `
Embed a QR code as binary data into an image using DCT.

Usages:
- encrypt text <input> <key> qrcode binary embed <input-image>;
- encrypt text <input> <key> qrcode binary embed <input-image> <output-image>;
`,
	Do: func(x *bonzai.Cmd, args ...string) error {
		fmt.Println("--- Embedding QR Code into JPEG ---")
		fmt.Println(args)

		if len(args) < 1 {
			return fmt.Errorf("missing input image path")
		}
		inputImage := args[0]

		qrData := vars.Fetch(EncryptEnv, EncryptDataVar, "zoo fall")

		outputImage := vars.Fetch(EmbeddedImagePathEnv, EmbeddedImagePathVar, "/tmp/embedded-image.jpg")
		if len(args) > 1 {
			outputImage = args[1]
		}

		err := EmbedQRCodeInJPEG(inputImage, outputImage, qrData)
		if err != nil {
			return fmt.Errorf("failed to embed QR code in JPEG: %w", err)
		}

		fmt.Println("QR code embedded in:", outputImage)
		return nil
	},
}
View Source
var EncryptCmd = &bonzai.Cmd{
	Name:  "encrypt",
	Alias: "e",
	Short: `encrypt information`,
	Comp:  comp.Cmds,
	Cmds: []*bonzai.Cmd{
		TextCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
}
View Source
var ExtractCmd = &bonzai.Cmd{
	Name:  "extract",
	Alias: `x`,
	Short: "extract hidden message",
	Long: `
Extract a hidden message from an image using StegHide and decode Reed-Solomon.

Usage:
encrypt extract <image.jpg> <password>`,
	Do: func(_ *bonzai.Cmd, args ...string) error {
		if len(args) < 2 {
			return fmt.Errorf("usage: stegcli extract <image.jpg> <password>")
		}

		imageFile := args[0]
		password := args[1]

		cmd := exec.Command("steghide", "extract", "-sf", imageFile, "-xf", "extracted_data.txt", "-p", password)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("error running steghide: %v", err)
		}

		extractedBytes, err := os.ReadFile("extracted_data.txt")
		if err != nil {
			return fmt.Errorf("error reading extracted file: %v", err)
		}

		encodedData := strings.TrimSpace(string(extractedBytes))
		encodedData = strings.ReplaceAll(encodedData, "\n", "")
		encodedData = strings.ReplaceAll(encodedData, "\r", "")

		fmt.Println("Extracted Base64 data:", encodedData)

		decodedBytes, err := base64.StdEncoding.DecodeString(encodedData)
		if err != nil {
			return fmt.Errorf("error decoding Base64: %v", err)
		}

		decodedMessage, err := decodeReedSolomon(string(decodedBytes))
		if err != nil {
			return fmt.Errorf("error decoding Reed-Solomon: %v", err)
		}

		fmt.Println("Final Decoded message:", decodedMessage)

		err = os.WriteFile("extracted_data.txt", []byte(decodedMessage), 0644)
		if err != nil {
			return fmt.Errorf("error writing cleaned extracted data: %v", err)
		}

		return nil
	},
}
View Source
var QRCodeCmd = &bonzai.Cmd{
	Name:  `qrcode`,
	Alias: `qr`,
	Comp:  comp.Cmds,
	Short: `qrcode related commands`,
	Cmds: []*bonzai.Cmd{

		CreateQRBinaryCmd,

		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Vars: bonzai.Vars{
		{
			K: QRDataVar,
			V: `foo`,
			E: QREnv,
			S: `binary data representing a qrcode`,
			P: true,
		},
	},
	Long: `
create QRCode holding data. Either just create one and return it, or create and transform in a binary data.

Usages:
- encrypt text <input> <key> qrcode create;
- encrypt text <input> <key> qrcode create binary;
- encrypt text <input> <key> qrcode binary;
`,
	Do: func(x *bonzai.Cmd, args ...string) error {

		data := vars.Fetch(EncryptEnv, EncryptDataVar, "zoo fall")

		err := qrcode.WriteFile(data, qrcode.Highest, 256, "/tmp/qr.png")
		fmt.Print("Wrote qrcode to /tmp/qr.png")
		if err != nil {
			return err
		}

		switch args[0] {
		case CreateQRBinaryCmd.Name:
			CreateQRBinaryCmd.Do(x, args[1:]...)
		}
		return nil
	},
}
View Source
var TextCmd = &bonzai.Cmd{
	Name:  "text",
	Alias: "t",
	Comp:  comp.Cmds,
	Short: "encrypt text using AES",
	Cmds: []*bonzai.Cmd{
		QRCodeCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Vars: bonzai.Vars{
		{
			K: EncryptDataVar,
			V: `foo`,
			E: EncryptEnv,
			S: `data to be used and transformed in encryption steps`,
			P: true,
		},
	},
	Long: `
encrypt text using AES.

Usage: encrypt text <input> <key>; in which |key|>15 characters
`,
	Do: func(x *bonzai.Cmd, args ...string) error {
		if len(args) < 2 {
			return fmt.Errorf("usage: encrypt text <input> <key>")
		}
		if len(args[1]) < 16 {
			return fmt.Errorf("key (password) must be greater or equal to 16 characters")
		}

		encrypted, err := EncryptMessage(args[0], args[1])
		vars.Data.Set(EncryptDataVar, encrypted)

		if err != nil {
			return err
		}

		if len(args) > 2 {
			fmt.Println(args[2:])

			QRCodeCmd.Do(x, args[3:]...)

		}

		return nil
	},
}

Functions

func CreateQRCodeBytes

func CreateQRCodeBytes(data string) ([]byte, error)

CreateQRCodeBytes generates a QR code and returns its PNG bytes.

func EmbedQRCodeInJPEG

func EmbedQRCodeInJPEG(inputPath, outputPath, qrData string) error

EmbedQRCodeInJPEG embeds a QR code bitstream into a JPEG's DCT coefficients

func EncryptMessage

func EncryptMessage(secret, key string) (string, error)

**🔹 Encrypt AES (Ensure Output is Correct)**

func ExtractBitstreamFromPNG

func ExtractBitstreamFromPNG(pngData []byte) ([]byte, error)

ExtractBitstreamFromPNG takes a PNG byte array and returns a packed bitstream.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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