Skip to content

0xfakeSpike/safeconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SafeConfig

一个安全的 Go 配置管理库,自动加密 JSON 配置文件中的敏感信息。

你只需要照常填写你的json文件,然后使用safeconfig.LoadAndEncrypt()函数读取,之后你会发现全部string和int字段都被加密了😊 当然你时不时需要修改某个字段,完全没有任何问题,你只需要将新的值填入其中就好,再次读取,明文字段又被加密了😁

Just write your JSON file as usual, then use safeconfig.LoadAndEncrypt() to read it. You'll find all string and int fields are automatically encrypted 😊 Need to modify a field? No problem! Just update the value in plain text, and the next time you read it, the plain text fields will be encrypted again 😁

特性

  • 🔐 自动加密/解密json中的字符串和数值字段
  • 🔄 读取时自动加密未加密的字段
  • 🎯 支持字符串、数字、数组、嵌套对象
  • 🔑 支持环境变量设置密钥

安装

go get github.com/0xfakespike/safeconfig

快速开始

package main

import (
    "fmt"
    "log"
    
    "github.com/0xfakespike/safeconfig"
)

type Config struct {
    DatabaseURL string `json:"database_url"`
    APIKey      string `json:"api_key"`
    Port        int    `json:"port"`
}

func main() {
    config := Config{
        DatabaseURL: "postgres://user:pass@localhost/db",
        APIKey:      "secret-api-key",
        Port:        8080,
    }
    
    if err := safeconfig.Write("./config.json", &config); err != nil {
        log.Fatal(err)
    }
    
    var loadedConfig Config
    if err := safeconfig.LoadAndEncrypt("./config.json", &loadedConfig); err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Database URL: %s\n", loadedConfig.DatabaseURL)
}

设置密钥

export SAFECONFIG_KEY="your-32-byte-encryption-key-here!!!!!"

或代码中设置(不推荐生产环境):

safeconfig.SetEncryptionKey("your-32-byte-encryption-key-here!!!!!")

API

  • LoadAndEncrypt(path string, v interface{}) error - 读取配置(自动解密并加密未加密字段)
  • Write(path string, v interface{}) error - 写入配置(自动加密)
  • Encrypt(plaintext string) (string, error) - 加密字符串
  • Decrypt(ciphertext string) (string, error) - 解密字符串

示例

查看 examples/main.go 获取更多示例。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages