encrypt

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EncryptBlockSize int64 = 1024 * 10
)

Variables

View Source
var Cmd = &cli.Command{
	Category: "Crypto",
	Name:     "encrypt",
	Usage:    "Encrypt data by aes and compress by zstd",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "key",
			Aliases: []string{"k"},
			Usage:   "Secret Key",
			Value:   "",
		},
		&cli.StringFlag{
			Name:  "iv",
			Usage: "Secret IV",
			Value: "",
		},
		&cli.BoolFlag{
			Name:  "no-compress",
			Usage: "Encrypt but not compress",
		},
		&cli.BoolFlag{
			Name:    "check",
			Aliases: []string{"c"},
			Usage:   "Detect whether the file is encrypted",
		},
	},
	Action: func(c *cli.Context) (err error) {
		if c.Bool("check") {
			return Check(c.Args().First())
		}
		key := c.String("key")
		if len(key) == 0 {
			conf := viper.New()
			conf.AddConfigPath(path.Join("$HOME", ".config", "virzz"))
			conf.SetConfigName("encrypt")
			conf.SetConfigType("yaml")
			if err := conf.ReadInConfig(); err != nil {
				logger.Debug(err)
			}
			key = conf.GetString("key")
			if len(key) == 0 {
				logger.Error("key is empty")
				key = utils.RandomStringByLength(32)
				logger.DebugF("key: %s", key)
				conf.Set("key", key)
				if err = conf.WriteConfig(); err != nil {
					logger.Debug(err)
					if err = conf.SafeWriteConfig(); err != nil {
						logger.Debug(err)
					}
				}
				logger.Success("Generate new key and save to config")
			}
		}
		err = Encrypt(c.Args().First(), []byte(key), []byte(c.String("iv")), !c.Bool("no-compress"))
		if err != nil {
			logger.DebugF("%+v", err)
		}
		return err
	},
}
View Source
var ErrNotEncryptedByVirzz = errors.New("Not Encrypted By virzz")

Functions

func AesCTREncrypt

func AesCTREncrypt(data, key, iv []byte) ([]byte, error)

func Check

func Check(filename string) error

func Compress

func Compress(filepath string, unCompress bool) error

func Decrypt

func Decrypt(filename string, key, iv []byte, efi *EncryptFileInfo) error

func Encrypt

func Encrypt(filename string, key, iv []byte, compress bool) error

Types

type EncryptFileInfo

type EncryptFileInfo struct {
	Size       uint32
	Block      uint32
	Ext        []byte
	IsCompress bool
}

func (*EncryptFileInfo) Data

func (e *EncryptFileInfo) Data() (int, []byte)

func (*EncryptFileInfo) Read

func (e *EncryptFileInfo) Read(data []byte) (*EncryptFileInfo, error)

func (*EncryptFileInfo) ReadFile

func (e *EncryptFileInfo) ReadFile(fp string) (*EncryptFileInfo, error)

Jump to

Keyboard shortcuts

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