Skip to content

Commit

Permalink
Prune
Browse files Browse the repository at this point in the history
Prune KeySizeError
Prune WriteKeyToPem return values
Prune unused manual dependency get in travis build
Prune unused function paramter

Signed-off-by: davidliu <david-khala@hotmail.com>
  • Loading branch information
davidkhala committed Jul 30, 2020
1 parent 445d8b3 commit 2e84520
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 376 deletions.
12 changes: 5 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
sudo: false
dist: bionic
language: go
os:
- linux
- osx
osx_image: xcode11
go:
- 1.14.x
- 1.12.x
- 1.13.x
before_install:
- export GO111MODULE=on
install:
- go get -u golang.org/x/lint/golint
- export golint=$(go list -f {{.Target}} golang.org/x/lint/golint)
- go get -v golang.org/x/crypto/chacha20poly1305
- go get -v golang.org/x/net/context
- go get -v google.golang.org/grpc/credentials
- go get -v github.com/golang/protobuf/proto
- go get -v golang.org/x/text/secure/bidirule
- go get -v google.golang.org/genproto/googleapis/rpc/status
- go mod vendor
- go build -v ./sm2
- go build -v ./sm3
Expand Down
347 changes: 161 additions & 186 deletions API使用说明.md
Original file line number Diff line number Diff line change
@@ -1,187 +1,162 @@
# 国密GM/T Go API使用说明

## 国密gmsm包安装

```bash
go get -u github.com/Hyperledger-TWGC/tj-gmsm
```

## SM3密码杂凑算法 - SM3 cryptographic hash algorithm

遵循的SM3标准号为: GM/T 0004-2012

导入包
```Go
import github.com/Hyperledger-TWGC/tj-gmsm/sm3
```

### 代码示例

```Go
data := "test"
h := sm3.New()
h.Write([]byte(data))
sum := h.Sum(nil)
fmt.Printf("digest value is: %x\n",sum)
```
### 方法列表

#### New
创建哈希计算实例
```Go
func New() hash.Hash
```

#### Sum
返回SM3哈希算法摘要值
```Go
func Sum() []byte
```

## SM4分组密码算法 - SM4 block cipher algorithm

遵循的SM4标准号为: GM/T 0002-2012

导入包
```Go
import github.com/Hyperledger-TWGC/tj-gmsm/sm4
```

### 代码示例

```Go
import "crypto/cipher"
import "github.com/Hyperledger-TWGC/tj-gmsm/sm4"

func main(){
// 128比特密钥
key := []byte("1234567890abcdef")
// 128比特iv
iv := make([]byte, sm4.BlockSize)
data := []byte("Tongji Fintech Research Institute")
ciphertxt,err := sm4Encrypt(key,iv, data)
if err != nil{
log.Fatal(err)
}
fmt.Printf("加密结果: %x\n", ciphertxt)
}

func sm4Encrypt(key, iv, plainText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData := pkcs5Padding(plainText, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return cryted, nil
}

func sm4Decrypt(key, iv, cipherText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(cipherText))
blockMode.CryptBlocks(origData, cipherText)
origData = pkcs5UnPadding(origData)
return origData, nil
}
// pkcs5填充
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}

func pkcs5UnPadding(src []byte) []byte {
length := len(src)
if(length==0){
return nil
}
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
```

### 方法列表

#### NewCipher
创建SM4密码分组算法模型,参数key长度只支持128比特。
```Go
func NewCipher(key []byte) (cipher.Block, error)
```

## SM2椭圆曲线公钥密码算法 - Public key cryptographic algorithm SM2 based on elliptic curves

遵循的SM2标准号为: GM/T 0003.1-2012、GM/T 0003.2-2012、GM/T 0003.3-2012、GM/T 0003.4-2012、GM/T 0003.5-2012、GM/T 0009-2012、GM/T 0010-2012

导入包
```Go
import github.com/Hyperledger-TWGC/tj-gmsm/sm2
```

### 代码示例

```Go
priv, err := sm2.GenerateKey() // 生成密钥对
if err != nil {
log.Fatal(err)
}
msg := []byte("Tongji Fintech Research Institute")
pub := &priv.PublicKey
ciphertxt, err := pub.Encrypt(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("加密结果:%x\n",ciphertxt)
plaintxt,err := priv.Decrypt(ciphertxt)
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(msg,plaintxt){
log.Fatal("原文不匹配")
}

r,s,err := sm2.Sign(priv, msg)
if err != nil {
log.Fatal(err)
}
isok := sm2.Verify(pub,msg,r,s)
fmt.Printf("Verified: %v\n", isok)
```

### 方法列表

#### GenerateKey
生成随机秘钥。
```Go
func GenerateKey() (*PrivateKey, error)
```

#### Sign
用私钥签名数据,成功返回以两个大数表示的签名结果,否则返回错误。
```Go
func Sign(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
```

#### Verify
用公钥验证数据签名, 验证成功返回True,否则返回False。
```Go
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool
```

#### Encrypt
用公钥加密数据,成功返回密文错误,否则返回错误。
```Go
func Encrypt(pub *PublicKey, data []byte) ([]byte, error)
```

#### Decrypt
用私钥解密数据,成功返回原始明文数据,否则返回错误。
```Go
func Decrypt(priv *PrivateKey, data []byte) ([]byte, error)
# 国密GM/T Go API使用说明

## 国密gmsm包安装

```bash
go get -u github.com/Hyperledger-TWGC/tj-gmsm
```

## SM3密码杂凑算法 - SM3 cryptographic hash algorithm
- 遵循的SM3标准号为: GM/T 0004-2012
- g package:`github.com/Hyperledger-TWGC/tj-gmsm/sm3`
- `type SM3 struct` 是原生接口hash.Hash的一个实现

### 代码示例

```Go
data := "test"
h := sm3.New()
h.Write([]byte(data))
sum := h.Sum(nil)
fmt.Printf("digest value is: %x\n",sum)
```

## SM4分组密码算法 - SM4 block cipher algorithm

- 遵循的SM4标准号为: GM/T 0002-2012
- go package:`github.com/Hyperledger-TWGC/tj-gmsm/sm4`

### 代码示例

```Go
import "crypto/cipher"
import "github.com/Hyperledger-TWGC/tj-gmsm/sm4"

func main(){
// 128比特密钥
key := []byte("1234567890abcdef")
// 128比特iv
iv := make([]byte, sm4.BlockSize)
data := []byte("Tongji Fintech Research Institute")
ciphertxt,err := sm4Encrypt(key,iv, data)
if err != nil{
log.Fatal(err)
}
fmt.Printf("加密结果: %x\n", ciphertxt)
}

func sm4Encrypt(key, iv, plainText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData := pkcs5Padding(plainText, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return cryted, nil
}

func sm4Decrypt(key, iv, cipherText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(cipherText))
blockMode.CryptBlocks(origData, cipherText)
origData = pkcs5UnPadding(origData)
return origData, nil
}
// pkcs5填充
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}

func pkcs5UnPadding(src []byte) []byte {
length := len(src)
if(length==0){
return nil
}
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
```

### 方法列表

#### NewCipher
创建SM4密码分组算法模型,参数key长度只支持128比特。
```Go
func NewCipher(key []byte) (cipher.Block, error)
```

## SM2椭圆曲线公钥密码算法 - Public key cryptographic algorithm SM2 based on elliptic curves

- 遵循的SM2标准号为: GM/T 0003.1-2012、GM/T 0003.2-2012、GM/T 0003.3-2012、GM/T 0003.4-2012、GM/T 0003.5-2012、GM/T 0009-2012、GM/T 0010-2012
- go package: `github.com/Hyperledger-TWGC/tj-gmsm/sm2`

### 代码示例

```Go
priv, err := sm2.GenerateKey() // 生成密钥对
if err != nil {
log.Fatal(err)
}
msg := []byte("Tongji Fintech Research Institute")
pub := &priv.PublicKey
ciphertxt, err := pub.Encrypt(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("加密结果:%x\n",ciphertxt)
plaintxt,err := priv.Decrypt(ciphertxt)
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(msg,plaintxt){
log.Fatal("原文不匹配")
}

r,s,err := sm2.Sign(priv, msg)
if err != nil {
log.Fatal(err)
}
isok := sm2.Verify(pub,msg,r,s)
fmt.Printf("Verified: %v\n", isok)
```

### 方法列表

#### GenerateKey
生成随机秘钥。
```Go
func GenerateKey() (*PrivateKey, error)
```

#### Sign
用私钥签名数据,成功返回以两个大数表示的签名结果,否则返回错误。
```Go
func Sign(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
```

#### Verify
用公钥验证数据签名, 验证成功返回True,否则返回False。
```Go
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool
```

#### Encrypt
用公钥加密数据,成功返回密文错误,否则返回错误。
```Go
func Encrypt(pub *PublicKey, data []byte) ([]byte, error)
```

#### Decrypt
用私钥解密数据,成功返回原始明文数据,否则返回错误。
```Go
func Decrypt(priv *PrivateKey, data []byte) ([]byte, error)
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GM SM2/3/4 library based on Golang

基于Go语言的国密SM2/SM3/SM4加密算法库

[![Build Status](https://travis-ci.com/Hyperledger-TWGC/gmsm.svg?branch=dev-fabric)](https://travis-ci.com/Hyperledger-TWGC/gmsm)
[![Build Status](https://travis-ci.com/Hyperledger-TWGC/tj-gmsm.svg?branch=dev-fabric)](https://travis-ci.com/Hyperledger-TWGC/tj-gmsm)
[![Build Status](https://dev.azure.com/Hyperledger/TWGC/_apis/build/status/Hyperledger-TWGC.tj-gmsm?branchName=dev-fabric)](https://dev.azure.com/Hyperledger/TWGC/_build/latest?definitionId=122&branchName=dev-fabric)
## Feature
GMSM包含以下主要功能
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ go 1.14

require (
github.com/golang/protobuf v1.3.3
github.com/tjfoc/gmtls v1.2.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6 // indirect
google.golang.org/grpc v1.30.0
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)
Loading

0 comments on commit 2e84520

Please sign in to comment.