1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

...

14 Commits

Author SHA1 Message Date
dudaodong
56c9327a86 release v2.2.7 2023-10-07 11:40:38 +08:00
dudaodong
11eb559998 update readme file 2023-10-07 11:15:14 +08:00
dudaodong
61a612a06a update: update Enqueue and Dequeue 2023-10-07 10:53:45 +08:00
dudaodong
2351ab4714 Merge branch 'v2' of github.com:duke-git/lancet into v2 2023-09-24 19:09:31 +08:00
dudaodong
88eec858b4 test: add test file 2023-09-24 19:09:19 +08:00
dudaodong
14c37b5a5f Merge branch 'v2' of github.com:duke-git/lancet into v2 2023-09-23 13:48:40 +08:00
dudaodong
ad8b1d424c Merge branch 'main' into v2 2023-09-23 13:48:25 +08:00
flytutu
a9f01d8a69 [opt] currentPath support windows and linux (#139)
Co-authored-by: hesu <hesu@eacomp.com>
2023-09-23 13:45:21 +08:00
o98k
781b89d51a feat(fileutil): add ReadFile func (#140) 2023-09-23 13:45:02 +08:00
dudaodong
073c77e751 refactor: remove unused code 2023-09-19 17:44:49 +08:00
dudaodong
91a0d3077d doc: update cryptor doc 2023-09-17 16:22:47 +08:00
dudaodong
17b34f8f19 doc: add doc for GenerateRsaKeyPair, RsaEncryptOAEP, RsaDecryptOAEP 2023-09-14 14:15:10 +08:00
dudaodong
8ff37f0eff feat: add GenerateRsaKeyPair, RsaEncryptOAEP, RsaDecryptOAEP 2023-09-14 14:06:54 +08:00
elza
f445ecbaf8 feat: add IsNotBlank func (#132)
Co-authored-by: yuanyou <yuanyou@kezaihui.com>
2023-09-13 15:29:26 +08:00
22 changed files with 716 additions and 107 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.2.6-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.2.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -38,7 +38,7 @@
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
``` ```
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.1. </b> 2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.2. </b>
```go ```go
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
@@ -451,6 +451,12 @@ import "github.com/duke-git/lancet/v2/cryptor"
- **<big>RsaDecrypt</big>** : decrypt data with ras algorithm. - **<big>RsaDecrypt</big>** : decrypt data with ras algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaDecrypt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>GenerateRsaKeyPair</big>** : creates rsa private and public key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#GenerateRsaKeyPair)]
- **<big>RsaEncryptOAEP</big>** : encrypts the given data with RSA-OAEP.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaEncryptOAEP)]
- **<big>RsaDecryptOAEP</big>** : decrypts the data with RSA-OAEP
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaDecryptOAEP)]
<h3 id="datetime"> 7. Datetime package supports date and time format and compare. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="datetime"> 7. Datetime package supports date and time format and compare. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -703,6 +709,8 @@ import "github.com/duke-git/lancet/v2/fileutil"
- **<big>WriteStringToFile</big>** : write string to target file. - **<big>WriteStringToFile</big>** : write string to target file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteStringToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteStringToFile)]
[[play](https://go.dev/play/p/GhLS6d8lH_g)] [[play](https://go.dev/play/p/GhLS6d8lH_g)]
- **<big>ReadFile</big>** : read file or url.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#ReadFile)]
<h3 id="formatter"> 10. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="formatter"> 10. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1543,6 +1551,8 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>IsBlank</big>** : checks if a string is whitespace or empty. - **<big>IsBlank</big>** : checks if a string is whitespace or empty.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsBlank)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsBlank)]
[[play](https://go.dev/play/p/6zXRH_c0Qd3)] [[play](https://go.dev/play/p/6zXRH_c0Qd3)]
- **<big>IsNotBlank</big>** : checks if a string is not whitespace or not empty.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsNotBlank)]
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings. - **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)]
[[play](https://go.dev/play/p/8UUTl2C5slo)] [[play](https://go.dev/play/p/8UUTl2C5slo)]

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.2.6-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.2.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -37,7 +37,7 @@
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
``` ```
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.1。</b> 2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.2。</b>
```go ```go
go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本 go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本
@@ -451,6 +451,12 @@ import "github.com/duke-git/lancet/v2/cryptor"
- **<big>RsaDecrypt</big>** : 用私钥文件 rsa 解密数据。 - **<big>RsaDecrypt</big>** : 用私钥文件 rsa 解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaDecrypt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>GenerateRsaKeyPair</big>** : 创建rsa公钥私钥和key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#GenerateRsaKeyPair)]
- **<big>RsaEncryptOAEP</big>** : rsa OAEP加密。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaEncryptOAEP)]
- **<big>RsaDecryptOAEP</big>** : rsa OAEP解密。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaDecryptOAEP)]
<h3 id="datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -707,6 +713,8 @@ import "github.com/duke-git/lancet/v2/fileutil"
- **<big>WriteStringToFile</big>** : 将字符串写入文件。 - **<big>WriteStringToFile</big>** : 将字符串写入文件。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteStringToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteStringToFile)]
[[play](https://go.dev/play/p/GhLS6d8lH_g)] [[play](https://go.dev/play/p/GhLS6d8lH_g)]
- **<big>ReadFile</big>** : 读取文件或者URL。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#ReadFile)]
<h3 id="formatter"> 10. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="formatter"> 10. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1550,6 +1558,8 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>IsBlank</big>** : 检查字符串是否为空格或空。 - **<big>IsBlank</big>** : 检查字符串是否为空格或空。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#IsBlank)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#IsBlank)]
[[play](https://go.dev/play/p/6zXRH_c0Qd3)] [[play](https://go.dev/play/p/6zXRH_c0Qd3)]
- **<big>IsNotBlank</big>** : 检查字符串是否不为空。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#IsNotBlank)]
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。 - **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#HasPrefixAny)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#HasPrefixAny)]
[[play](https://go.dev/play/p/8UUTl2C5slo)] [[play](https://go.dev/play/p/8UUTl2C5slo)]

View File

@@ -13,6 +13,7 @@ import (
"crypto/des" "crypto/des"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/sha256"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io" "io"
@@ -505,3 +506,32 @@ func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
} }
return plainText return plainText
} }
// GenerateRsaKeyPair create rsa private and public key.
// Play: todo
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) {
privateKey, _ := rsa.GenerateKey(rand.Reader, keySize)
return privateKey, &privateKey.PublicKey
}
// RsaEncryptOAEP encrypts the given data with RSA-OAEP.
// Play: todo
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) {
encryptedBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &key, data, label)
if err != nil {
return nil, err
}
return encryptedBytes, nil
}
// RsaDecryptOAEP decrypts the data with RSA-OAEP.
// Play: todo
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) {
decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, &key, ciphertext, label)
if err != nil {
return nil, err
}
return decryptedBytes, nil
}

View File

@@ -1,6 +1,8 @@
package cryptor package cryptor
import "fmt" import (
"fmt"
)
func ExampleAesEcbEncrypt() { func ExampleAesEcbEncrypt() {
data := "hello" data := "hello"
@@ -484,3 +486,25 @@ func ExampleSha512WithBase64() {
// Output: // Output:
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw== // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
} }
func ExampleRsaEncryptOAEP() {
pri, pub := GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := RsaEncryptOAEP(data, label, *pub)
if err != nil {
return
}
decrypted, err := RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
return
}
fmt.Println(string(decrypted))
// Output:
// hello world
}

View File

@@ -150,3 +150,21 @@ func TestRsaEncrypt(t *testing.T) {
assert := internal.NewAssert(t, "TestRsaEncrypt") assert := internal.NewAssert(t, "TestRsaEncrypt")
assert.Equal(string(data), string(decrypted)) assert.Equal(string(data), string(decrypted))
} }
func TestRsaEncryptOAEP(t *testing.T) {
assert := internal.NewAssert(t, "TestRsaEncrypt")
t.Parallel()
pri, pub := GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := RsaEncryptOAEP(data, label, *pub)
assert.IsNil(err)
decrypted, err := RsaDecryptOAEP([]byte(encrypted), label, *pri)
assert.IsNil(err)
assert.Equal("hello world", string(decrypted))
}

View File

@@ -12,7 +12,7 @@ import (
// ArrayQueue implements queue with slice // ArrayQueue implements queue with slice
type ArrayQueue[T any] struct { type ArrayQueue[T any] struct {
items []T data []T
head int head int
tail int tail int
capacity int capacity int
@@ -21,7 +21,7 @@ type ArrayQueue[T any] struct {
func NewArrayQueue[T any](capacity int) *ArrayQueue[T] { func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {
return &ArrayQueue[T]{ return &ArrayQueue[T]{
items: make([]T, 0, capacity), data: make([]T, 0, capacity),
head: 0, head: 0,
tail: 0, tail: 0,
capacity: capacity, capacity: capacity,
@@ -33,7 +33,7 @@ func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {
func (q *ArrayQueue[T]) Data() []T { func (q *ArrayQueue[T]) Data() []T {
items := []T{} items := []T{}
for i := q.head; i < q.tail; i++ { for i := q.head; i < q.tail; i++ {
items = append(items, q.items[i]) items = append(items, q.data[i])
} }
return items return items
} }
@@ -55,40 +55,71 @@ func (q *ArrayQueue[T]) IsFull() bool {
// Front return front value of queue // Front return front value of queue
func (q *ArrayQueue[T]) Front() T { func (q *ArrayQueue[T]) Front() T {
return q.items[0] return q.data[0]
} }
// Back return back value of queue // Back return back value of queue
func (q *ArrayQueue[T]) Back() T { func (q *ArrayQueue[T]) Back() T {
return q.items[q.size-1] return q.data[q.size-1]
} }
// EnQueue put element into queue // EnQueue put element into queue
func (q *ArrayQueue[T]) Enqueue(item T) bool { func (q *ArrayQueue[T]) Enqueue(item T) bool {
if q.head == 0 && q.tail == q.capacity { if q.tail < q.capacity {
return false q.data = append(q.data, item)
} else if q.head != 0 && q.tail == q.capacity { // q.tail++
for i := q.head; i < q.tail; i++ { q.data[q.tail] = item
q.items[i-q.head] = q.items[i] } else {
//upgrade
if q.head > 0 {
for i := 0; i < q.tail-q.head; i++ {
q.data[i] = q.data[i+q.head]
}
q.tail -= q.head
q.head = 0
} else {
if q.capacity < 65536 {
if q.capacity == 0 {
q.capacity = 1
}
q.capacity *= 2
} else {
q.capacity += 2 ^ 16
}
tmp := make([]T, q.capacity, q.capacity)
copy(tmp, q.data)
q.data = tmp
} }
q.tail = q.tail - q.head
q.head = 0 q.data[q.tail] = item
} }
q.items = append(q.items, item)
q.tail++ q.tail++
q.size++ q.size++
return true return true
} }
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error // DeQueue remove head element of queue and return it, if queue is empty, return nil and error
func (q *ArrayQueue[T]) Dequeue() (T, bool) { func (q *ArrayQueue[T]) Dequeue() (T, bool) {
var item T var item T
if q.head == q.tail { if q.size == 0 {
return item, false return item, false
} }
item = q.items[q.head]
item = q.data[q.head]
q.head++ q.head++
if q.head >= 1024 || q.head*2 > q.tail {
q.capacity -= q.head
q.tail -= q.head
tmp := make([]T, q.capacity, q.capacity)
copy(tmp, q.data[q.head:])
q.data = tmp
q.head = 0
}
q.size-- q.size--
return item, true return item, true
} }
@@ -96,7 +127,7 @@ func (q *ArrayQueue[T]) Dequeue() (T, bool) {
// Clear the queue data // Clear the queue data
func (q *ArrayQueue[T]) Clear() { func (q *ArrayQueue[T]) Clear() {
capacity := q.capacity capacity := q.capacity
q.items = make([]T, 0, capacity) q.data = make([]T, 0, capacity)
q.head = 0 q.head = 0
q.tail = 0 q.tail = 0
q.size = 0 q.size = 0
@@ -105,7 +136,7 @@ func (q *ArrayQueue[T]) Clear() {
// Contain checks if the value is in queue or not // Contain checks if the value is in queue or not
func (q *ArrayQueue[T]) Contain(value T) bool { func (q *ArrayQueue[T]) Contain(value T) bool {
for _, v := range q.items { for _, v := range q.data {
if reflect.DeepEqual(v, value) { if reflect.DeepEqual(v, value) {
return true return true
} }
@@ -117,7 +148,7 @@ func (q *ArrayQueue[T]) Contain(value T) bool {
func (q *ArrayQueue[T]) Print() { func (q *ArrayQueue[T]) Print() {
info := "[" info := "["
for i := q.head; i < q.tail; i++ { for i := q.head; i < q.tail; i++ {
info += fmt.Sprintf("%+v, ", q.items[i]) info += fmt.Sprintf("%+v, ", q.data[i])
} }
info += "]" info += "]"
fmt.Println(info) fmt.Println(info)

View File

@@ -11,7 +11,7 @@ func TestArrayQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Enqueue") assert := internal.NewAssert(t, "TestArrayQueue_Enqueue")
queue := NewArrayQueue[int](5) queue := NewArrayQueue[int](2)
queue.Enqueue(1) queue.Enqueue(1)
queue.Enqueue(2) queue.Enqueue(2)
queue.Enqueue(3) queue.Enqueue(3)

View File

@@ -1,6 +1,6 @@
# Cryptor # Cryptor
cryptor包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。 cryptor 包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -65,6 +65,9 @@ import (
- [GenerateRsaKey](#GenerateRsaKey) - [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt) - [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt) - [RsaDecrypt](#RsaDecrypt)
- [GenerateRsaKeyPair](#GenerateRsaKeyPair)
- [RsaEncryptOAEP](#RsaEncryptOAEP)
- [RsaDecryptOAEP](#RsaDecryptOAEP)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -988,13 +991,13 @@ import (
func main() { func main() {
str := "hello" str := "hello"
key := "12345" key := "12345"
hms := cryptor.HmacSha512WithBase64(str, key) hms := cryptor.HmacSha512WithBase64(str, key)
fmt.Println(hms) fmt.Println(hms)
// Output: // Output:
// 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A== // 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
} }
``` ```
@@ -1051,10 +1054,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5StringWithBase64("hello") md5Str := cryptor.Md5StringWithBase64("hello")
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// XUFAKrxLKna5cZ2REBfFkg== // XUFAKrxLKna5cZ2REBfFkg==
} }
``` ```
@@ -1080,10 +1083,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5Byte([]byte{'a'}) md5Str := cryptor.Md5Byte([]byte{'a'})
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// 0cc175b9c0f1b6a831c399e269772661 // 0cc175b9c0f1b6a831c399e269772661
} }
``` ```
@@ -1109,10 +1112,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5ByteWithBase64([]byte("hello")) md5Str := cryptor.Md5ByteWithBase64([]byte("hello"))
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// XUFAKrxLKna5cZ2REBfFkg== // XUFAKrxLKna5cZ2REBfFkg==
} }
``` ```
@@ -1195,10 +1198,10 @@ import (
func main() { func main() {
result := cryptor.Sha1WithBase64("hello") result := cryptor.Sha1WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// qvTGHdzF6KLavt4PO0gs2a6pQ00= // qvTGHdzF6KLavt4PO0gs2a6pQ00=
} }
``` ```
@@ -1255,10 +1258,10 @@ import (
func main() { func main() {
result := cryptor.Sha256WithBase64("hello") result := cryptor.Sha256WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
} }
``` ```
@@ -1315,10 +1318,10 @@ import (
func main() { func main() {
result := cryptor.Sha512WithBase64("hello") result := cryptor.Sha512WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw== // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
} }
``` ```
@@ -1423,3 +1426,114 @@ func main() {
// hello // hello
} }
``` ```
### <span id="GenerateRsaKeyPair">GenerateRsaKeyPair</span>
<p>创建rsa公钥私钥和key。</p>
<b>函数签名:</b>
```go
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
}
```
### <span id="RsaEncryptOAEP">RsaEncryptOAEP</span>
<p>rsa OAEP加密。</p>
<b>函数签名:</b>
```go
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub)
if err != nil {
return
}
decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
return
}
fmt.Println(string(decrypted))
// Output:
// hello world
}
```
### <span id="RsaDecryptOAEP">RsaDecryptOAEP</span>
<p>rsa OAEP解密。</p>
<b>函数签名:</b>
```go
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub)
if err != nil {
return
}
decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
return
}
fmt.Println(string(decrypted))
// Output:
// hello world
}
```

View File

@@ -47,6 +47,7 @@ import (
- [WriteCsvFile](#WriteCsvFile) - [WriteCsvFile](#WriteCsvFile)
- [WriteStringToFile](#WriteStringToFile) - [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile) - [WriteBytesToFile](#WriteBytesToFile)
- [ReadFile](#ReadFile)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -839,3 +840,41 @@ func main() {
// hello // hello
} }
``` ```
### <span id="ReadFile">ReadFile</span>
<p>读取文件或者URL</p>
<b>函数签名:</b>
```go
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
```
<b>示例:<span style="float:right;display:inline-block;"></span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
return
}
defer fn()
dat, err := io.ReadAll(reader)
if err != nil {
return
}
fmt.Println(string(dat))
// Output:
// User-agent: *
// Disallow: /deny
}
```

View File

@@ -49,6 +49,7 @@ import (
- [StringToBytes](#StringToBytes) - [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString) - [BytesToString](#BytesToString)
- [IsBlank](#IsBlank) - [IsBlank](#IsBlank)
- [IsNotBlank](#IsNotBlank)
- [HasPrefixAny](#HasPrefixAny) - [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny) - [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset) - [IndexOffset](#IndexOffset)
@@ -1075,6 +1076,45 @@ func main() {
} }
``` ```
### <span id="IsNotBlank">IsNotBlank</span>
<p>Checks if a string is not whitespace or not empty.</p>
<b>Signature:</b>
```go
func IsNotBlank(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
```
### <span id="HasPrefixAny">HasPrefixAny</span> ### <span id="HasPrefixAny">HasPrefixAny</span>
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p> <p>检查字符串是否以指定字符串数组中的任何一个开头。</p>

View File

@@ -1,18 +1,18 @@
# Cryptor # Cryptor
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa. Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
- [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/cryptor" "github.com/duke-git/lancet/v2/cryptor"
@@ -23,49 +23,51 @@ import (
## Index ## Index
- [AesEcbEncrypt](#AesEcbEncrypt) - [AesEcbEncrypt](#AesEcbEncrypt)
- [AesEcbDecrypt](#AesEcbDecrypt) - [AesEcbDecrypt](#AesEcbDecrypt)
- [AesCbcEncrypt](#AesCbcEncrypt) - [AesCbcEncrypt](#AesCbcEncrypt)
- [AesCbcDecrypt](#AesCbcDecrypt) - [AesCbcDecrypt](#AesCbcDecrypt)
- [AesCtrCrypt](#AesCtrCrypt) - [AesCtrCrypt](#AesCtrCrypt)
- [AesCfbEncrypt](#AesCfbEncrypt) - [AesCfbEncrypt](#AesCfbEncrypt)
- [AesCfbDecrypt](#AesCfbDecrypt) - [AesCfbDecrypt](#AesCfbDecrypt)
- [AesOfbEncrypt](#AesOfbEncrypt) - [AesOfbEncrypt](#AesOfbEncrypt)
- [AesOfbDecrypt](#AesOfbDecrypt) - [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode) - [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode) - [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt) - [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt) - [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt) - [DesCbcEncrypt](#DesCbcEncrypt)
- [DesCbcDecrypt](#DesCbcDecrypt) - [DesCbcDecrypt](#DesCbcDecrypt)
- [DesCtrCrypt](#DesCtrCrypt) - [DesCtrCrypt](#DesCtrCrypt)
- [DesCfbEncrypt](#DesCfbEncrypt) - [DesCfbEncrypt](#DesCfbEncrypt)
- [DesCfbDecrypt](#DesCfbDecrypt) - [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt) - [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5) - [HmacMd5](#HmacMd5)
- [HmacMd5WithBase64](#HmacMd5WithBase64) - [HmacMd5WithBase64](#HmacMd5WithBase64)
- [HmacSha1](#HmacSha1) - [HmacSha1](#HmacSha1)
- [HmacSha1WithBase64](#HmacSha1WithBase64) - [HmacSha1WithBase64](#HmacSha1WithBase64)
- [HmacSha256](#HmacSha256) - [HmacSha256](#HmacSha256)
- [HmacSha256WithBase64](#HmacSha256WithBase64) - [HmacSha256WithBase64](#HmacSha256WithBase64)
- [HmacSha512](#HmacSha512) - [HmacSha512](#HmacSha512)
- [HmacSha512WithBase64](#HmacSha512WithBase64) - [HmacSha512WithBase64](#HmacSha512WithBase64)
- [Md5String](#Md5String) - [Md5String](#Md5String)
- [Md5StringWithBase64](#Md5StringWithBase64) - [Md5StringWithBase64](#Md5StringWithBase64)
- [Md5Byte](#Md5Byte) - [Md5Byte](#Md5Byte)
- [Md5ByteWithBase64](#Md5ByteWithBase64) - [Md5ByteWithBase64](#Md5ByteWithBase64)
- [Md5File](#Md5File) - [Md5File](#Md5File)
- [Sha1](#Sha1) - [Sha1](#Sha1)
- [Sha1WithBase64](#Sha1WithBase64) - [Sha1WithBase64](#Sha1WithBase64)
- [Sha256](#Sha256) - [Sha256](#Sha256)
- [Sha256WithBase64](#Sha256WithBase64) - [Sha256WithBase64](#Sha256WithBase64)
- [Sha512](#Sha512) - [Sha512](#Sha512)
- [Sha512WithBase64](#Sha512WithBase64) - [Sha512WithBase64](#Sha512WithBase64)
- [GenerateRsaKey](#GenerateRsaKey) - [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt) - [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt) - [RsaDecrypt](#RsaDecrypt)
- [GenerateRsaKeyPair](#GenerateRsaKeyPair)
- [RsaEncryptOAEP](#RsaEncryptOAEP)
- [RsaDecryptOAEP](#RsaDecryptOAEP)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -80,6 +82,7 @@ import (
```go ```go
func AesEcbEncrypt(data, key []byte) []byte func AesEcbEncrypt(data, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
@@ -113,6 +116,7 @@ func main() {
```go ```go
func AesEcbDecrypt(encrypted, key []byte) []byte func AesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
@@ -146,6 +150,7 @@ func main() {
```go ```go
func AesCbcEncrypt(data, key []byte) []byte func AesCbcEncrypt(data, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
@@ -339,6 +344,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="AesCfbDecrypt">AesOfbDecrypt</span> ### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -382,6 +388,7 @@ func main() {
```go ```go
func Base64StdEncode(s string) string func Base64StdEncode(s string) string
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/VOaUyQUreoK)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/VOaUyQUreoK)</span></b>
```go ```go
@@ -400,6 +407,7 @@ func main() {
// aGVsbG8= // aGVsbG8=
} }
``` ```
### <span id="Base64StdDecode">Base64StdDecode</span> ### <span id="Base64StdDecode">Base64StdDecode</span>
<p>Decode a base64 encoded string.</p> <p>Decode a base64 encoded string.</p>
@@ -463,6 +471,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesEcbDecrypt">DesEcbDecrypt</span> ### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
@@ -565,6 +574,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCtrCrypt">DesCtrCrypt</span> ### <span id="DesCtrCrypt">DesCtrCrypt</span>
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p> <p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
@@ -632,6 +642,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCfbDecrypt">DesCfbDecrypt</span> ### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
@@ -665,6 +676,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbEncrypt">DesOfbEncrypt</span> ### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -698,6 +710,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbDecrypt">DesOfbDecrypt</span> ### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -988,7 +1001,6 @@ func main() {
} }
``` ```
### <span id="Md5String">Md5String</span> ### <span id="Md5String">Md5String</span>
<p>Get the md5 value of string.</p> <p>Get the md5 value of string.</p>
@@ -1366,11 +1378,11 @@ func main() {
if err != nil { if err != nil {
return return
} }
data := []byte("hello") data := []byte("hello")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) fmt.Println(string(decrypted))
// Output: // Output:
@@ -1378,7 +1390,6 @@ func main() {
} }
``` ```
### <span id="RsaDecrypt">RsaDecrypt</span> ### <span id="RsaDecrypt">RsaDecrypt</span>
<p>Decrypt data with private key file useing ras algorithm.</p> <p>Decrypt data with private key file useing ras algorithm.</p>
@@ -1404,11 +1415,11 @@ func main() {
if err != nil { if err != nil {
return return
} }
data := []byte("hello") data := []byte("hello")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) fmt.Println(string(decrypted))
// Output: // Output:
@@ -1416,5 +1427,113 @@ func main() {
} }
``` ```
### <span id="GenerateRsaKeyPair">GenerateRsaKeyPair</span>
<p>Creates rsa private and public key.</p>
<b>Signature:</b>
```go
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
}
```
### <span id="RsaEncryptOAEP">RsaEncryptOAEP</span>
<p>Encrypts the given data with RSA-OAEP.</p>
<b>Signature:</b>
```go
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub)
if err != nil {
return
}
decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
return
}
fmt.Println(string(decrypted))
// Output:
// hello world
}
```
### <span id="RsaDecryptOAEP">RsaDecryptOAEP</span>
<p>Decrypts the data with RSA-OAEP.</p>
<b>Signature:</b>
```go
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
data := []byte("hello world")
label := []byte("123456")
encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub)
if err != nil {
return
}
decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
return
}
fmt.Println(string(decrypted))
// Output:
// hello world
}
```

View File

@@ -47,6 +47,7 @@ import (
- [WriteCsvFile](#WriteCsvFile) - [WriteCsvFile](#WriteCsvFile)
- [WriteStringToFile](#WriteStringToFile) - [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile) - [WriteBytesToFile](#WriteBytesToFile)
- [ReadFile](#ReadFile)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -839,3 +840,41 @@ func main() {
// hello // hello
} }
``` ```
### <span id="ReadFile">ReadFile</span>
<p>Read File/URL</p>
<b>Signature:</b>
```go
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
```
<b>Example:<span style="float:right;display:inline-block;"> </span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
return
}
defer fn()
dat, err := io.ReadAll(reader)
if err != nil {
return
}
fmt.Println(string(dat))
// Output:
// User-agent: *
// Disallow: /deny
}
```

View File

@@ -49,6 +49,7 @@ import (
- [StringToBytes](#StringToBytes) - [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString) - [BytesToString](#BytesToString)
- [IsBlank](#IsBlank) - [IsBlank](#IsBlank)
- [IsNotBlank](#IsNotBlank)
- [HasPrefixAny](#HasPrefixAny) - [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny) - [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset) - [IndexOffset](#IndexOffset)
@@ -1076,6 +1077,45 @@ func main() {
} }
``` ```
### <span id="IsNotBlank">IsNotBlank</span>
<p>Checks if a string is not whitespace or not empty.</p>
<b>Signature:</b>
```go
func IsNotBlank(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
```
### <span id="HasPrefixAny">HasPrefixAny</span> ### <span id="HasPrefixAny">HasPrefixAny</span>
<p>Checks if a string starts with any of an array of specified strings.</p> <p>Checks if a string starts with any of an array of specified strings.</p>

View File

@@ -18,10 +18,11 @@ import (
"io/fs" "io/fs"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"github.com/duke-git/lancet/v2/validator"
) )
// IsExist checks if a file or directory exists. // IsExist checks if a file or directory exists.
@@ -491,7 +492,7 @@ func CurrentPath() string {
var absPath string var absPath string
_, filename, _, ok := runtime.Caller(1) _, filename, _, ok := runtime.Caller(1)
if ok { if ok {
absPath = path.Dir(filename) absPath = filepath.Dir(filename)
} }
return absPath return absPath
@@ -624,3 +625,24 @@ func WriteBytesToFile(filepath string, content []byte) error {
_, err = f.Write(content) _, err = f.Write(content)
return err return err
} }
// ReadFile get file reader by a url or a local file
// Play: todo
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) {
switch {
case validator.IsUrl(path):
resp, err := http.Get(path)
if err != nil {
return nil, func() {}, err
}
return resp.Body, func() { resp.Body.Close() }, nil
case IsExist(path):
reader, err := os.Open(path)
if err != nil {
return nil, func() {}, err
}
return reader, func() { reader.Close() }, nil
default:
return nil, func() {}, errors.New("unknown file type")
}
}

View File

@@ -2,6 +2,7 @@ package fileutil
import ( import (
"fmt" "fmt"
"io"
"os" "os"
) )
@@ -385,3 +386,20 @@ func ExampleWriteBytesToFile() {
// Output: // Output:
// hello // hello
} }
func ExampleReadFile() {
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
return
}
defer fn()
dat, err := io.ReadAll(reader)
if err != nil {
return
}
fmt.Println(string(dat))
// Output:
// User-agent: *
// Disallow: /deny
}

View File

@@ -1,6 +1,7 @@
package fileutil package fileutil
import ( import (
"io"
"os" "os"
"testing" "testing"
@@ -457,3 +458,21 @@ func TestWriteBytesToFile(t *testing.T) {
os.Remove(filepath) os.Remove(filepath)
} }
func TestReadFile(t *testing.T) {
reader, close, err := ReadFile("https://httpbin.org/robots.txt")
if err != nil {
t.Fail()
}
defer close()
dat, err := io.ReadAll(reader)
if err != nil {
t.Fail()
}
want := `User-agent: *
Disallow: /deny
`
internal.NewAssert(t, "TestReadFile").Equal(want, string(dat))
}

View File

@@ -1,2 +1,3 @@
Lili,22,female Lili,22,female
Jim,21,male Jim,21,male
1 Lili 22 female
2 Jim 21 male
3

View File

@@ -1,2 +1,3 @@
Lili,22,female Lili,22,female
Jim,21,male Jim,21,male
1 Lili 22 female
2 Jim 21 male
3

View File

@@ -29,14 +29,11 @@ func RandInt(min, max int) int {
if min == max { if min == max {
return min return min
} }
if max < min { if max < min {
min, max = max, min min, max = max, min
} }
// fix: https://github.com/duke-git/lancet/issues/75
// r := rand.New(rand.NewSource(time.Now().UnixNano()))
// return r.Intn(max-min) + min
return rand.Intn(max-min) + min return rand.Intn(max-min) + min
} }

View File

@@ -408,6 +408,11 @@ func IsBlank(str string) bool {
return true return true
} }
// IsNotBlank checks if a string is not whitespace, not empty.
func IsNotBlank(str string) bool {
return !IsBlank(str)
}
// HasPrefixAny check if a string starts with any of a slice of specified strings. // HasPrefixAny check if a string starts with any of a slice of specified strings.
// Play: https://go.dev/play/p/8UUTl2C5slo // Play: https://go.dev/play/p/8UUTl2C5slo
func HasPrefixAny(str string, prefixes []string) bool { func HasPrefixAny(str string, prefixes []string) bool {

View File

@@ -479,6 +479,26 @@ func ExampleIsBlank() {
// false // false
} }
func ExampleIsNotBlank() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
func ExampleHasPrefixAny() { func ExampleHasPrefixAny() {
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"}) result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := HasPrefixAny("foo bar", []string{"oom", "world"}) result2 := HasPrefixAny("foo bar", []string{"oom", "world"})

View File

@@ -427,6 +427,18 @@ func TestIsBlank(t *testing.T) {
assert.Equal(IsBlank(" 中文"), false) assert.Equal(IsBlank(" 中文"), false)
} }
func TestIsNotBlank(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBlank")
assert.Equal(IsNotBlank(""), false)
assert.Equal(IsNotBlank(" "), false)
assert.Equal(IsNotBlank("\t\v\f\n"), false)
assert.Equal(IsNotBlank(" 中文"), true)
assert.Equal(IsNotBlank(" world "), true)
}
func TestHasPrefixAny(t *testing.T) { func TestHasPrefixAny(t *testing.T) {
t.Parallel() t.Parallel()