secstorage

package module
v1.0.0 Latest Latest
Warning

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

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

README

SecStorage

SecStorage 是一个基于 Go 语言的文件加密存储库,它提供了一种简单的方式来加密存储文件,同时确保数据的安全性。

这个项目的目标是将文件切割加密为多个块,这些块文件可以被自由且安全的放置在任何存储设备上,当然也包括云端存储。

使用

package main

import (
	"fmt"
	"log"

	secstorage "github.com/liulcode/secstorage/core"
)

func main() {
	// config, err := secstorage.LoadConfig(*configPath)
	// if err != nil {
	// 	log.Fatalf("Failed to load config: %v", err)
	// }

	storageDir := "./storage"
	ChunkSizeKB := 4096
	argon2Time := 1
	argon2MemoryKB := 65536
	argon2Threads := 4

	password := "testpassword" //密码,加密解密均需要,建议实际使用时从用户输入读取

	/**
	 * 如果使用配置文件,可以通过config获取配置信息(当然也可以自己定义处理)
	 * var configPath = flag.String("config", "config.yaml", "Path to the configuration file")
	 * config, err := secstorage.LoadConfig(*configPath)
	 * storageDir := config.StoragePath
	 * ChunkSizeKB := config.ChunkSizeKB
	 * argon2Time := config.Argon2.Time
	 * argon2MemoryKB := config.Argon2.MemoryKB
	 * argon2Threads := config.Argon2.Threads
	 **/

	syncer := secstorage.NewSyncer(storageDir)

	//加密文件
	manifestID, err := syncer.EncryptFile("./testfile.txt", password, ChunkSizeKB, uint32(argon2Time), uint32(argon2MemoryKB), uint8(argon2Threads))
	//此处manifestID为加密后的文件目录,后续解密需要传递这个目录(可任意修改)
	if err != nil {
		log.Fatalf("加密文件是不: %v", err)
	}
	fmt.Printf("加密文件成功. 加密后的文件目录为: %s\n", manifestID)

	//解密还原文件
	if err := syncer.DecryptFile(manifestID, "./abc.txt", password); err != nil {
		log.Fatalf("还原文件失败: %v", err)
	}
	fmt.Println("还原文件成功")
}

Thanks

这是一个 AI 生成项目,感谢科技的进步。

项目代码大部分由 AI 生成,魔法咒语在 prompt 目录中提供。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argon2Config

type Argon2Config struct {
	// Time 是 Argon2 算法的迭代次数。
	Time uint32 `yaml:"time"`
	// MemoryKB 是 Argon2 算法应使用的内存量(以 KB 为单位)。
	MemoryKB uint32 `yaml:"memory_kb"`
	// Threads 是 Argon2 算法可以使用的 CPU 线程数。
	Threads uint8 `yaml:"threads"`
}

type Config

type Config struct {
	// ChunkSizeKB 定义了内容分块的平均大小(以 KB 为单位)。
	ChunkSizeKB int `yaml:"chunk_size_kb"`
	// DataShards 定义了纠删码所需的数据分片数。
	DataShards int `yaml:"data_shards"`
	// ParityShards 定义了纠删码所需的奇偶校验分片数。
	ParityShards int `yaml:"parity_shards"`
	// Argon2 包含了用于密钥派生的 Argon2 算法的配置。
	Argon2 Argon2Config `yaml:"argon2"`
	// StoragePath 定义了加密文件存储的根目录。
	StoragePath string `yaml:"storage_path"`
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig 从指定的路径加载 YAML 配置文件并解析它。 它返回一个包含配置的 Config 结构体指针,或者在出错时返回一个错误。

type EncryptionOptions

type EncryptionOptions struct {
	Password      string
	DataShards    int
	ParityShards  int
	ChunkSizeKB   int
	Argon2Time    uint32
	Argon2Memory  uint32
	Argon2Threads uint8
}

EncryptionOptions 封装了加密操作所需的所有参数。

type Manifest

type Manifest struct {
	Salt                     []byte     `json:"salt"`
	ChunkPaths               []string   `json:"chunk_paths"`
	EncryptedOrigFilename    []byte     `json:"encrypted_orig_filename"`
	EncryptedDataKeys        [][]byte   `json:"encrypted_data_keys"`
	Argon2Time               uint32     `json:"argon2_time"`
	Argon2Memory             uint32     `json:"argon2_memory"`
	Argon2Threads            uint8      `json:"argon2_threads"`
	Signature                []byte     `json:"signature,omitempty"`
	DataShards               int        `json:"data_shards"`
	ParityShards             int        `json:"parity_shards"`
	ErasureCodeChunkSuffixes [][]string `json:"erasure_code_chunk_suffixes"`
	EncryptedChunkSizes      []int      `json:"encrypted_chunk_sizes"`
}

Manifest 结构体定义了加密文件的元数据,这些元数据以 JSON 格式存储在 manifest.json 文件中。 它包含了重建和解密文件所需的所有信息。

type SecureSyncer

type SecureSyncer interface {
	EncryptFile(localPath string, opts EncryptionOptions) (string, error)
	DecryptFile(manifestID, outputPath, password string) error
}

SecureSyncer 定义了安全文件同步器的接口,提供了加密和解密文件的核心功能。

type Syncer

type Syncer struct {
	StorageDir string
}

Syncer 是 SecureSyncer 接口的具体实现。

func NewSyncer

func NewSyncer(storageDir string) *Syncer

NewSyncer 创建一个新的 Syncer 实例。

func (*Syncer) DecryptFile

func (s *Syncer) DecryptFile(manifestID, outputPath, password string) (err error)

DecryptFile 负责从存储中解密文件。

func (*Syncer) EncryptFile

func (s *Syncer) EncryptFile(localPath string, opts EncryptionOptions) (manifestID string, err error)

EncryptFile 负责加密单个文件,并将其安全地存储到指定的目录中。

Jump to

Keyboard shortcuts

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