Skip to content

Commit

Permalink
Wed Apr 10 10:35:09 CST 2024
Browse files Browse the repository at this point in the history
Signed-off-by: yakumioto <yaku.mioto@gmail.com>
  • Loading branch information
yakumioto committed Apr 10, 2024
1 parent 42c12db commit c3be6e8
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 148 deletions.
179 changes: 142 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,182 @@
[![codecov](https://codecov.io/gh/yakumioto/go-crypto-suite/graph/badge.svg?token=HqETyi1zYV)](https://codecov.io/gh/yakumioto/go-crypto-suite)
[![actions](https://github.com/yakumioto/go-crypto-suite/actions/workflows/ci.yaml/badge.svg)](https://github.com/yakumioto/go-crypto-suite/actions)

一个快速易用方便的 Go 语言的加密工具库、采用统一的接口实现支持密钥生成、密钥导入导出、加密解密、签名验签、等功能。[README in English](README_en.md)
A quick, easy-to-use, and convenient Go language cryptography toolkit that provides a unified interface for key generation, key import/export, encryption/decryption, signing/verification, password hashing, and other functionality.

## Features

Based on Go's generics feature, it supports `[]byte` and `string`.

Unified output format:

- Ciphertext format: {algorithm identifier}.{ciphertext}, {algorithm identifier}.{ciphertext}.{signature}.
- Ciphertext + signature format: {algorithm identifier}.{ciphertext}.{signature}
- Signature format: {algorithm identifier}.{message digest}.{signature}
- Hash format: {algorithm identifier}.{hash value}

## Supported Algorithms

| Hasher | Encryption & Decryption | Signing & Verification | Hashing & Verification |
|:------------|:-----------------------:|:----------------------:|:----------------------:|
| AES_CBC_128 || | |
| AES_CBC_192 || | |
| AES_CBC_256 || | |
| AES_GCM_128 || | |
| AES_GCM_192 || | |
| AES_GCM_256 || | |
| Chacha20 || | |
| XChacha20 || | |
| RSA_1024 ||| |
| RSA_2048 ||| |
| RSA_4096 ||| |
| ECDSA_P256 | || |
| ECDSA_P384 | || |
| ECDSA_P521 | || |
| HMAC_SHA256 | || |
| HMAC_SHA512 | || |
| ARGON2I | | ||
| ARGON2ID | | ||
| PBKDF2_SHA256 | | ||
| PBKDF2_SHA512 | | ||

## Installation

## 特征
```
go get github.com/yakumioto/go-crypto-suite
```

基于 Go 范型特性,支持 `[]byte``string`
## Usage Examples

统一的密文格式:{算法标识}.{密文}、{算法标识}.{消息摘要}.{签名}、{算法标识}.{密文}.{签名}。
Encryption: Using `AES_GCM_256` to encrypt and decrypt strings

```go
package main

- 支持对称密钥
- AES CBC(128位、192位、256位)
- AES GCM(128位、192位、256位)
- 密钥导入
- 支持非对称密钥
- ECDSA(P256、P384)
- RSA(1024位、2048位、4096位)
- 密钥生成
- 密钥导入
- 支持哈希算法
- HMAC SHA(SHA256、SHA512)
- 密钥导入
import (
"fmt"

## 安装
"github.com/yakumioto/go-crypto-suite"
"github.com/yakumioto/go-crypto-suite/types"
)

func main() {
key, err := crypto.KeyImport[string](types.AesGcm256, "123456")
if err != nil {
panic(err)
}

ciphertext, err := key.Encrypt("hello world")
if err != nil {
panic(err)
}
fmt.Println(ciphertext)
// aes_gcm_256.RYrO4e+d2xslDQgZiZWQgClwVr/jZygLb3VMP5COwvxOBg6OSpHf

plaintext, err := key.Decrypt(ciphertext)
if err != nil {
panic(err)
}

fmt.Println(plaintext)
// hello world
}
```
go get github.com/yakumioto/go-crypto-suite
```

## 使用
Signing: Using `ECDSA_P256` to sign and verify strings

```go
package main

import (
"fmt"

下面是一个基本的示例,演示了如何使用 AES-CBC-128 加密和解密字符串:
"github.com/yakumioto/go-crypto-suite"
"github.com/yakumioto/go-crypto-suite/types"
)

func main() {
privKey, err := crypto.KeyGenerate[string](types.EcdsaP256)
if err != nil {
panic(err)
}

signature, err := privKey.Sign("hello world")
if err != nil {
panic(err)
}
fmt.Println(signature)
// ecdsa_p256.uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek.MEQCIBFl8IcfPldpN5eTOW+rKmrTyLTx7zZsdFv56suUGy2VAiA9ZIBt7i9WmQwazwtpki5M+8oZlFBqovITQzykZDfQBA

pubKey, err := privKey.PublicKey()
if err != nil {
panic(err)
}

verified, err := pubKey.Verify("hello world", signature)
if err != nil {
panic(err)
}

fmt.Println(verified)
// true
}
```

Password Hashing: Using `ARGON2ID` to hash passwords

```go
package main

import (
"fmt"

"github.com/yakumioto/go-crypto-suite"
"github.com/yakumioto/go-crypto-suite/argon2"
"github.com/yakumioto/go-crypto-suite/types"
)

func main() {
key, err := crypto.KeyImport[string]("123456", crypto.AesCbc128)
key, err := crypto.KeyGenerate[string](types.Argon2,
argon2.WithMemory[string](65536),
argon2.WithTime[string](4),
argon2.WithThreads[string](4),
)
if err != nil {
panic(err)
panic(err)
}

ciphertext, err := key.Encrypt("hello world")
signature, err := key.Sign("hello world")
if err != nil {
panic(err)
panic(err)
}
fmt.Println(signature)
// argon2.argon2id$v=19$m=65536,t=4,p=4$BYzGXsTNx3Vy86vpqWU7+Q$AgOiaQEMnPudblmI4rTHSmFgZcNAgND4aQM+KwtdK40

plaintext, err := key.Decrypt(ciphertext)
verified, err := key.Verify("hello world", signature)
if err != nil {
panic(err)
panic(err)
}

fmt.Println(plaintext)
fmt.Println(verified)
// true
}
```

## 感谢
## Community

Telegram:<https://t.me/gocryptosuite>

## Acknowledgments

项目受到了以下项目的启发:
This project was inspired by the following projects:

- Hyperledger Fabric https://github.com/hyperledger/fabric
- Bitwardenhttps://bitwarden.com/help/bitwarden-security-white-paper
- Hyperledger Fabric: https://github.com/hyperledger/fabric
- Bitwarden: https://bitwarden.com/help/bitwarden-security-white-paper

## 贡献
## Contribution

欢迎贡献!请随时提交拉取请求或打开议题。
Contributions are welcome! Please feel free to submit pull requests or open issues.

## 许可证
## License

本项目采用 MIT 许可证。
This project is licensed under the MIT License.
83 changes: 0 additions & 83 deletions README_en.md

This file was deleted.

Loading

0 comments on commit c3be6e8

Please sign in to comment.