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

Compare commits

...

5 Commits

Author SHA1 Message Date
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
dudaodong
b698fec50f doc: add go playground demo 2023-09-11 15:59:35 +08:00
dudaodong
b38bb66b34 doc: update readme file 2023-09-11 11:51:45 +08:00
20 changed files with 666 additions and 191 deletions

View File

@@ -1325,6 +1325,9 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>Join</big>** : join the slice item with specify separator. - **<big>Join</big>** : join the slice item with specify separator.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)] [[play](https://go.dev/play/p/huKzqwNDD7V)]
- **<big>Partition</big>** : partition all slice elements with the evaluation of the given predicate functions.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Partition)]
[[play](https://go.dev/play/p/lkQ3Ri2NQhV)]
<h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1540,6 +1543,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)]
@@ -1826,6 +1831,33 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsPrintable</big>** : checks if string is all printable chars. - **<big>IsPrintable</big>** : checks if string is all printable chars.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPrintable)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)] [[play](https://go.dev/play/p/Pe1FE2gdtTP)]
- **<big>IsBin</big>** : check if a give string is a valid binary value or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsBin)]
[[play](https://go.dev/play/p/ogPeg2XJH4P)]
- **<big>IsHex</big>** : check if a give string is a valid hexadecimal value or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsHex)]
[[play](https://go.dev/play/p/M2qpHbEwmm7)]
- **<big>IsBase64URL</big>** : check if a give string is a valid URL-safe Base64 encoded string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsBase64URL)]
[[play](https://go.dev/play/p/vhl4mr8GZ6S)]
- **<big>IsJWT</big>** : check if a give string is a valid JSON Web Token (JWT).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsJWT)]
[[play](https://go.dev/play/p/R6Op7heJbKI)]
- **<big>IsVisa</big>** : check if a give string is a valid visa card nubmer or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsVisa)]
[[play](https://go.dev/play/p/SdS2keOyJsl)]
- **<big>IsMasterCard</big>** : check if a give string is a valid master card nubmer or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsMasterCard)]
[[play](https://go.dev/play/p/CwWBFRrG27b)]
- **<big>IsAmericanExpress</big>** : check if a give string is a valid american expression card nubmer or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsAmericanExpress)]
[[play](https://go.dev/play/p/HIDFpcOdpkd)]
- **<big>IsUnionPay</big>** : check if a give string is a valid union pay nubmer or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsUnionPay)]
[[play](https://go.dev/play/p/CUHPEwEITDf)]
- **<big>IsChinaUnionPay</big>** : check if a give string is a valid china union pay nubmer or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChinaUnionPay)]
[[play](https://go.dev/play/p/yafpdxLiymu)]
<h3 id="xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1892,7 +1924,7 @@ Hello, I am a software developer and have been engaged in development work for 1
<br/> <br/>
<br/> <br/>
*Donated funds will be used to maintain this website and pay for cloud server costs. Or just buy me a cup of ☕️ when I'm sleepy writing code.* *Donated funds will be used to maintain [lancet](https://www.golancet.cn/en/) website and pay for cloud server costs. Or just buy me a cup of ☕️ when I'm sleepy writing code.*
## Contributors ## Contributors

View File

@@ -18,7 +18,7 @@
lancet柳叶刀是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。 lancet柳叶刀是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
</p> </p>
## <a href="https://www.golancet.cn/" target="_blank">官网</a> | [English](./README.md) ## <a href="https://www.golancet.cn/" target="_blank">官网</a> | [English](./README.md)
## 特性 ## 特性
@@ -1329,6 +1329,9 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>Join</big>** : 用指定的分隔符链接切片元素。 - **<big>Join</big>** : 用指定的分隔符链接切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)] [[play](https://go.dev/play/p/huKzqwNDD7V)]
- **<big>Partition</big>** : 根据给定的predicate判断函数分组切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Partition)]
[[play](https://go.dev/play/p/lkQ3Ri2NQhV)]
<h3 id="stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1572,7 +1575,6 @@ import "github.com/duke-git/lancet/v2/strutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#RemoveWhiteSpace)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#RemoveWhiteSpace)]
[[play](https://go.dev/play/p/HzLC9vsTwkf)] [[play](https://go.dev/play/p/HzLC9vsTwkf)]
<h3 id="system"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="system"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
@@ -1833,6 +1835,33 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。 - **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPrintable)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)] [[play](https://go.dev/play/p/Pe1FE2gdtTP)]
- **<big>IsBin</big>** : 检查字符串是否是有效的二进制数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsBin)]
[[play](https://go.dev/play/p/ogPeg2XJH4P)]
- **<big>IsHex</big>** : 检查字符串是否是有效的十六进制数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsHex)]
[[play](https://go.dev/play/p/M2qpHbEwmm7)]
- **<big>IsBase64URL</big>** : 检查字符串是否是有效的 base64 url。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsBase64URL)]
[[play](https://go.dev/play/p/vhl4mr8GZ6S)]
- **<big>IsJWT</big>** : 检查字符串是否是有效的 JSON Web Token (JWT)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsJWT)]
[[play](https://go.dev/play/p/R6Op7heJbKI)]
- **<big>IsVisa</big>** : 检查字符串是否是有效的 visa 卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsVisa)]
[[play](https://go.dev/play/p/SdS2keOyJsl)]
- **<big>IsMasterCard</big>** : 检查字符串是否是有效的 MasterCard 卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsMasterCard)]
[[play](https://go.dev/play/p/CwWBFRrG27b)]
- **<big>IsAmericanExpress</big>** : 检查字符串是否是有效的 American Express 卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsAmericanExpress)]
[[play](https://go.dev/play/p/HIDFpcOdpkd)]
- **<big>IsUnionPay</big>** : 检查字符串是否是有效的美国银联卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsUnionPay)]
[[play](https://go.dev/play/p/CUHPEwEITDf)]
- **<big>IsChinaUnionPay</big>** : 检查字符串是否是有效的中国银联卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChinaUnionPay)]
[[play](https://go.dev/play/p/yafpdxLiymu)]
<h3 id="xerror"> 25. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="xerror"> 25. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1855,7 +1884,7 @@ import "github.com/duke-git/lancet/v2/xerror"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_Wrap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_Wrap)]
[[play](https://go.dev/play/p/RpjJ5u5sc97)] [[play](https://go.dev/play/p/RpjJ5u5sc97)]
- **<big>XError_Unwrap</big>** : 解构 XEerror 为 error 对象。适配 github.com/pkg/errors。 - **<big>XError_Unwrap</big>** : 解构 XEerror 为 error 对象。适配 github.com/pkg/errors。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_Unwrap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_Unwrap)
[[play](https://go.dev/play/p/VUXJ8BST4c6)] [[play](https://go.dev/play/p/VUXJ8BST4c6)]
- **<big>XError_With</big>** : 添加与 XError 对象的键和值。 - **<big>XError_With</big>** : 添加与 XError 对象的键和值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_With)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/xerror.md#XError_With)]
@@ -1888,7 +1917,7 @@ import "github.com/duke-git/lancet/v2/xerror"
## 赞助 ## 赞助
您好,我是一名软件开发者,从事开发工作 13 年。热爱软件开源。并愿意为此付出精力。是开源项目lancet的作者。Lancet 自两年前开源发布以来,已有超过 1000 个内外部项目使用。lancet一直会对所有用户免费。您的支持是对我继续奋斗的有力鼓励。谢谢! 微信扫描以下二维码或点击以下赞助按钮发起赞助。 您好,我是一名软件开发者,从事开发工作 13 年。热爱软件开源。并愿意为此付出精力。是开源项目 lancet 的作者。Lancet 自两年前开源发布以来,已有超过 1000 个内外部项目使用。lancet 一直会对所有用户免费。您的支持是对我继续奋斗的有力鼓励。谢谢! 微信扫描以下二维码或点击以下赞助按钮发起赞助。
<div style="position: relative;display: inline-block;"> <div style="position: relative;display: inline-block;">
<img src="./docs/public/wechat_pay.png" width="260" height="260"/> <img src="./docs/public/wechat_pay.png" width="260" height="260"/>
@@ -1898,12 +1927,11 @@ import "github.com/duke-git/lancet/v2/xerror"
<br/> <br/>
<br/> <br/>
*捐赠的资金将用于后续lancet官网的维护和云服务器的费用支付。或者当我写代码困倦时给我买杯 ☕️。* _捐赠的资金将用于后续[lancet](https://www.golancet.cn/)官网的维护和云服务器的费用支付。或者当我写代码困倦时,给我买杯 ☕️。_
## 贡献者 ## 贡献者
感谢所有为lancet贡献过代码的人 感谢所有为 lancet 贡献过代码的人!
<a href="https://github.com/duke-git/lancet/graphs/contributors"> <a href="https://github.com/duke-git/lancet/graphs/contributors">
<img src="https://contrib.rocks/image?repo=duke-git/lancet" /> <img src="https://contrib.rocks/image?repo=duke-git/lancet" />

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

@@ -27,7 +27,7 @@ outline: deep
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
line-height: 40px; line-height: 40px;
background: #ecefff; background: #10b981;
border: 1px solid; border: 1px solid;
margin-right: 10px; margin-right: 10px;
margin-bottom: 10px; margin-bottom: 10px;

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

@@ -2464,7 +2464,7 @@ func main() {
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/lkQ3Ri2NQhV)</span></b>
```go ```go
import ( import (

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

@@ -67,7 +67,6 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="ContainChinese">ContainChinese</span> ### <span id="ContainChinese">ContainChinese</span>
@@ -80,7 +79,7 @@ import (
func ContainChinese(s string) bool func ContainChinese(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/7DpU0uElYeM)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/7DpU0uElYeM)</span></b>
```go ```go
import ( import (
@@ -114,7 +113,7 @@ func main() {
func ContainLetter(str string) bool func ContainLetter(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/lqFD04Yyewp)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/lqFD04Yyewp)</span></b>
```go ```go
import ( import (
@@ -148,7 +147,7 @@ func main() {
func ContainLower(str string) bool func ContainLower(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Srqi1ItvnAA)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Srqi1ItvnAA)</span></b>
```go ```go
import ( import (
@@ -182,7 +181,7 @@ func main() {
func ContainUpper(str string) bool func ContainUpper(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/CmWeBEk27-z)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/CmWeBEk27-z)</span></b>
```go ```go
import ( import (
@@ -216,7 +215,7 @@ func main() {
func IsAlpha(s string) bool func IsAlpha(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/7Q5sGOz2izQ)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/7Q5sGOz2izQ)</span></b>
```go ```go
import ( import (
@@ -250,7 +249,7 @@ func main() {
func IsAllUpper(str string) bool func IsAllUpper(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/ZHctgeK1n4Z)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/ZHctgeK1n4Z)</span></b>
```go ```go
import ( import (
@@ -284,7 +283,7 @@ func main() {
func IsAllLower(str string) bool func IsAllLower(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/GjqCnOfV6cM)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/GjqCnOfV6cM)</span></b>
```go ```go
import ( import (
@@ -318,7 +317,7 @@ func main() {
func IsASCII(str string) bool func IsASCII(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/hfQNPLX0jNa)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/hfQNPLX0jNa)</span></b>
```go ```go
import ( import (
@@ -358,7 +357,7 @@ func main() {
func IsBase64(base64 string) bool func IsBase64(base64 string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/sWHEySAt6hl)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/sWHEySAt6hl)</span></b>
```go ```go
import ( import (
@@ -389,7 +388,7 @@ func main() {
func IsChineseMobile(mobileNum string) bool func IsChineseMobile(mobileNum string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/GPYUlGTOqe3)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/GPYUlGTOqe3)</span></b>
```go ```go
import ( import (
@@ -420,7 +419,7 @@ func main() {
func IsChineseIdNum(id string) bool func IsChineseIdNum(id string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/d8EWhl2UGDF)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/d8EWhl2UGDF)</span></b>
```go ```go
import ( import (
@@ -451,7 +450,7 @@ func main() {
func IsChinesePhone(phone string) bool func IsChinesePhone(phone string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/RUD_-7YZJ3I)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/RUD_-7YZJ3I)</span></b>
```go ```go
import ( import (
@@ -482,7 +481,7 @@ func main() {
func IsCreditCard(creditCart string) bool func IsCreditCard(creditCart string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/sNwwL6B0-v4)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/sNwwL6B0-v4)</span></b>
```go ```go
import ( import (
@@ -513,7 +512,7 @@ func main() {
func IsDns(dns string) bool func IsDns(dns string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/jlYApVLLGTZ)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/jlYApVLLGTZ)</span></b>
```go ```go
import ( import (
@@ -547,7 +546,7 @@ func main() {
func IsEmail(email string) bool func IsEmail(email string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Os9VaFlT33G)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Os9VaFlT33G)</span></b>
```go ```go
import ( import (
@@ -578,7 +577,7 @@ func main() {
func IsEmptyString(s string) bool func IsEmptyString(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/dpzgUjFnBCX)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/dpzgUjFnBCX)</span></b>
```go ```go
import ( import (
@@ -612,7 +611,7 @@ func main() {
func IsInt(v any) bool func IsInt(v any) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/eFoIHbgzl-z)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/eFoIHbgzl-z)</span></b>
```go ```go
import ( import (
@@ -649,7 +648,7 @@ func main() {
func IsFloat(v any) bool func IsFloat(v any) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/vsyG-sxr99_Z)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/vsyG-sxr99_Z)</span></b>
```go ```go
import ( import (
@@ -686,7 +685,7 @@ func main() {
func IsNumber(v any) bool func IsNumber(v any) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/mdJHOAvtsvF)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/mdJHOAvtsvF)</span></b>
```go ```go
import ( import (
@@ -713,7 +712,6 @@ func main() {
} }
``` ```
### <span id="IsIntStr">IsIntStr</span> ### <span id="IsIntStr">IsIntStr</span>
<p>验证字符串是否是可以转换为整数。</p> <p>验证字符串是否是可以转换为整数。</p>
@@ -724,7 +722,7 @@ func main() {
func IsIntStr(s string) bool func IsIntStr(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/jQRtFv-a0Rk)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/jQRtFv-a0Rk)</span></b>
```go ```go
import ( import (
@@ -761,7 +759,7 @@ func main() {
func IsFloatStr(s string) bool func IsFloatStr(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/LOYwS_Oyl7U)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/LOYwS_Oyl7U)</span></b>
```go ```go
import ( import (
@@ -798,7 +796,7 @@ func main() {
func IsNumberStr(s string) bool func IsNumberStr(s string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/LzaKocSV79u)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/LzaKocSV79u)</span></b>
```go ```go
import ( import (
@@ -835,7 +833,7 @@ func main() {
func IsJSON(str string) bool func IsJSON(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/8Kip1Itjiil)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/8Kip1Itjiil)</span></b>
```go ```go
import ( import (
@@ -872,7 +870,7 @@ func main() {
func IsRegexMatch(s, regex string) bool func IsRegexMatch(s, regex string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/z_XeZo_litG)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/z_XeZo_litG)</span></b>
```go ```go
import ( import (
@@ -893,8 +891,6 @@ func main() {
} }
``` ```
### <span id="IsIp">IsIp</span> ### <span id="IsIp">IsIp</span>
<p>验证字符串是否是ip地址。</p> <p>验证字符串是否是ip地址。</p>
@@ -905,7 +901,7 @@ func main() {
func IsIp(ipstr string) bool func IsIp(ipstr string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/FgcplDvmxoD)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/FgcplDvmxoD)</span></b>
```go ```go
import ( import (
@@ -942,7 +938,7 @@ func main() {
func IsIpV4(ipstr string) bool func IsIpV4(ipstr string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/zBGT99EjaIu)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/zBGT99EjaIu)</span></b>
```go ```go
import ( import (
@@ -973,7 +969,7 @@ func main() {
func IsIpV6(ipstr string) bool func IsIpV6(ipstr string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/AHA0r0AzIdC)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/AHA0r0AzIdC)</span></b>
```go ```go
import ( import (
@@ -1004,7 +1000,7 @@ func main() {
func IsStrongPassword(password string, length int) bool func IsStrongPassword(password string, length int) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/QHdVcSQ3uDg)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/QHdVcSQ3uDg)</span></b>
```go ```go
import ( import (
@@ -1035,7 +1031,7 @@ func main() {
func IsUrl(str string) bool func IsUrl(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/pbJGa7F98Ka)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/pbJGa7F98Ka)</span></b>
```go ```go
import ( import (
@@ -1070,7 +1066,7 @@ func main() {
func IsWeakPassword(password string, length int) bool func IsWeakPassword(password string, length int) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/wqakscZH5gH)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/wqakscZH5gH)</span></b>
```go ```go
import ( import (
@@ -1101,7 +1097,7 @@ func main() {
func IsZeroValue(value any) bool func IsZeroValue(value any) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/UMrwaDCi_t4)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/UMrwaDCi_t4)</span></b>
```go ```go
import ( import (
@@ -1138,7 +1134,7 @@ func main() {
func IsGBK(data []byte) bool func IsGBK(data []byte) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/E2nt3unlmzP)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/E2nt3unlmzP)</span></b>
```go ```go
import ( import (
@@ -1160,7 +1156,6 @@ func main() {
} }
``` ```
### <span id="IsPrintable">IsPrintable</span> ### <span id="IsPrintable">IsPrintable</span>
<p>检查字符串是否全部为可打印字符。</p> <p>检查字符串是否全部为可打印字符。</p>
@@ -1171,7 +1166,7 @@ func main() {
func IsPrintable(str string) bool func IsPrintable(str string) bool
``` ```
<b>示例:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Pe1FE2gdtTP)</span></b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Pe1FE2gdtTP)</span></b>
```go ```go
import ( import (
@@ -1211,7 +1206,7 @@ func main() {
func IsBin(v string) bool func IsBin(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/ogPeg2XJH4P)</span></b>
```go ```go
import ( import (
@@ -1248,7 +1243,7 @@ func main() {
func IsHex(v string) bool func IsHex(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/M2qpHbEwmm7)</span></b>
```go ```go
import ( import (
@@ -1285,7 +1280,7 @@ func main() {
func IsBase64URL(v string) bool func IsBase64URL(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/vhl4mr8GZ6S)</span></b>
```go ```go
import ( import (
@@ -1322,7 +1317,7 @@ func main() {
func IsJWT(v string) bool func IsJWT(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/R6Op7heJbKI)</span></b>
```go ```go
import ( import (
@@ -1353,7 +1348,7 @@ func main() {
func IsVisa(v string) bool func IsVisa(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/SdS2keOyJsl)</span></b>
```go ```go
import ( import (
@@ -1384,7 +1379,7 @@ func main() {
func IsMasterCard(v string) bool func IsMasterCard(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/CwWBFRrG27b)</span></b>
```go ```go
import ( import (
@@ -1415,7 +1410,7 @@ func main() {
func IsAmericanExpress(v string) bool func IsAmericanExpress(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/HIDFpcOdpkd)</span></b>
```go ```go
import ( import (
@@ -1436,7 +1431,7 @@ func main() {
} }
``` ```
### <span id="IsUnionPay">IsVisa</span> ### <span id="IsUnionPay">IsUnionPay</span>
<p>检查字符串是否是有效的美国银联卡号。</p> <p>检查字符串是否是有效的美国银联卡号。</p>
@@ -1446,7 +1441,7 @@ func main() {
func IsUnionPay(v string) bool func IsUnionPay(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/CUHPEwEITDf)</span></b>
```go ```go
import ( import (
@@ -1477,7 +1472,7 @@ func main() {
func IsChinaUnionPay(v string) bool func IsChinaUnionPay(v string) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/yafpdxLiymu)</span></b>
```go ```go
import ( import (

View File

@@ -27,7 +27,7 @@ outline: deep
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
line-height: 40px; line-height: 40px;
background: #ecefff; background: #059669;
border: 1px solid; border: 1px solid;
margin-right: 10px; margin-right: 10px;
margin-bottom: 10px; margin-bottom: 10px;

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,48 @@ 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)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -80,6 +79,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 +113,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 +147,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 +341,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 +385,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 +404,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 +468,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 +571,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 +639,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 +673,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 +707,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 +998,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 +1375,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 +1387,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 +1412,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 +1424,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

@@ -91,6 +91,7 @@ import (
- [Without](#Without) - [Without](#Without)
- [KeyBy](#KeyBy) - [KeyBy](#KeyBy)
- [Join](#Join) - [Join](#Join)
- [Partition](#Partition)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -2461,7 +2462,7 @@ func main() {
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/lkQ3Ri2NQhV)</span></b>
```go ```go
import ( import (

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

@@ -81,7 +81,7 @@ import (
func ContainChinese(s string) bool func ContainChinese(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/7DpU0uElYeM)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/7DpU0uElYeM)</span></b>
```go ```go
import ( import (
@@ -115,7 +115,7 @@ func main() {
func ContainLetter(str string) bool func ContainLetter(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/lqFD04Yyewp)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/lqFD04Yyewp)</span></b>
```go ```go
import ( import (
@@ -149,7 +149,7 @@ func main() {
func ContainLower(str string) bool func ContainLower(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Srqi1ItvnAA)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Srqi1ItvnAA)</span></b>
```go ```go
import ( import (
@@ -183,7 +183,7 @@ func main() {
func ContainUpper(str string) bool func ContainUpper(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/CmWeBEk27-z)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/CmWeBEk27-z)</span></b>
```go ```go
import ( import (
@@ -217,7 +217,7 @@ func main() {
func IsAlpha(s string) bool func IsAlpha(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/7Q5sGOz2izQ)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/7Q5sGOz2izQ)</span></b>
```go ```go
import ( import (
@@ -251,7 +251,7 @@ func main() {
func IsAllUpper(str string) bool func IsAllUpper(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/ZHctgeK1n4Z)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/ZHctgeK1n4Z)</span></b>
```go ```go
import ( import (
@@ -285,7 +285,7 @@ func main() {
func IsAllLower(str string) bool func IsAllLower(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/GjqCnOfV6cM)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/GjqCnOfV6cM)</span></b>
```go ```go
import ( import (
@@ -319,7 +319,7 @@ func main() {
func IsASCII(str string) bool func IsASCII(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/hfQNPLX0jNa)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/hfQNPLX0jNa)</span></b>
```go ```go
import ( import (
@@ -359,7 +359,7 @@ func main() {
func IsBase64(base64 string) bool func IsBase64(base64 string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/sWHEySAt6hl)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/sWHEySAt6hl)</span></b>
```go ```go
import ( import (
@@ -390,7 +390,7 @@ func main() {
func IsChineseMobile(mobileNum string) bool func IsChineseMobile(mobileNum string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/GPYUlGTOqe3)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/GPYUlGTOqe3)</span></b>
```go ```go
import ( import (
@@ -421,7 +421,7 @@ func main() {
func IsChineseIdNum(id string) bool func IsChineseIdNum(id string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/d8EWhl2UGDF)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/d8EWhl2UGDF)</span></b>
```go ```go
import ( import (
@@ -452,7 +452,7 @@ func main() {
func IsChinesePhone(phone string) bool func IsChinesePhone(phone string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/RUD_-7YZJ3I)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/RUD_-7YZJ3I)</span></b>
```go ```go
import ( import (
@@ -483,7 +483,7 @@ func main() {
func IsCreditCard(creditCart string) bool func IsCreditCard(creditCart string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/sNwwL6B0-v4)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/sNwwL6B0-v4)</span></b>
```go ```go
import ( import (
@@ -514,7 +514,7 @@ func main() {
func IsDns(dns string) bool func IsDns(dns string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/jlYApVLLGTZ)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/jlYApVLLGTZ)</span></b>
```go ```go
import ( import (
@@ -548,7 +548,7 @@ func main() {
func IsEmail(email string) bool func IsEmail(email string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Os9VaFlT33G)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Os9VaFlT33G)</span></b>
```go ```go
import ( import (
@@ -579,7 +579,7 @@ func main() {
func IsEmptyString(s string) bool func IsEmptyString(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/dpzgUjFnBCX)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/dpzgUjFnBCX)</span></b>
```go ```go
import ( import (
@@ -613,7 +613,7 @@ func main() {
func IsInt(v any) bool func IsInt(v any) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/eFoIHbgzl-z)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/eFoIHbgzl-z)</span></b>
```go ```go
import ( import (
@@ -650,7 +650,7 @@ func main() {
func IsFloat(v any) bool func IsFloat(v any) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/vsyG-sxr99_Z)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/vsyG-sxr99_Z)</span></b>
```go ```go
import ( import (
@@ -687,7 +687,7 @@ func main() {
func IsNumber(v any) bool func IsNumber(v any) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/mdJHOAvtsvF)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/mdJHOAvtsvF)</span></b>
```go ```go
import ( import (
@@ -724,7 +724,7 @@ func main() {
func IsIntStr(s string) bool func IsIntStr(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/jQRtFv-a0Rk)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/jQRtFv-a0Rk)</span></b>
```go ```go
import ( import (
@@ -761,7 +761,7 @@ func main() {
func IsFloatStr(s string) bool func IsFloatStr(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/LOYwS_Oyl7U)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/LOYwS_Oyl7U)</span></b>
```go ```go
import ( import (
@@ -798,7 +798,7 @@ func main() {
func IsNumberStr(s string) bool func IsNumberStr(s string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/LzaKocSV79u)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/LzaKocSV79u)</span></b>
```go ```go
import ( import (
@@ -835,7 +835,7 @@ func main() {
func IsJSON(str string) bool func IsJSON(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/8Kip1Itjiil)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/8Kip1Itjiil)</span></b>
```go ```go
import ( import (
@@ -872,7 +872,7 @@ func main() {
func IsRegexMatch(s, regex string) bool func IsRegexMatch(s, regex string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/z_XeZo_litG)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/z_XeZo_litG)</span></b>
```go ```go
import ( import (
@@ -903,7 +903,7 @@ func main() {
func IsIp(ipstr string) bool func IsIp(ipstr string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/FgcplDvmxoD)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/FgcplDvmxoD)</span></b>
```go ```go
import ( import (
@@ -940,7 +940,7 @@ func main() {
func IsIpV4(ipstr string) bool func IsIpV4(ipstr string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/zBGT99EjaIu)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/zBGT99EjaIu)</span></b>
```go ```go
import ( import (
@@ -971,7 +971,7 @@ func main() {
func IsIpV6(ipstr string) bool func IsIpV6(ipstr string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/AHA0r0AzIdC)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/AHA0r0AzIdC)</span></b>
```go ```go
import ( import (
@@ -1002,7 +1002,7 @@ func main() {
func IsStrongPassword(password string, length int) bool func IsStrongPassword(password string, length int) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/QHdVcSQ3uDg)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/QHdVcSQ3uDg)</span></b>
```go ```go
import ( import (
@@ -1033,7 +1033,7 @@ func main() {
func IsUrl(str string) bool func IsUrl(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/pbJGa7F98Ka)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/pbJGa7F98Ka)</span></b>
```go ```go
import ( import (
@@ -1068,7 +1068,7 @@ func main() {
func IsWeakPassword(password string, length int) bool func IsWeakPassword(password string, length int) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/wqakscZH5gH)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/wqakscZH5gH)</span></b>
```go ```go
import ( import (
@@ -1099,7 +1099,7 @@ func main() {
func IsZeroValue(value any) bool func IsZeroValue(value any) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/UMrwaDCi_t4)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/UMrwaDCi_t4)</span></b>
```go ```go
import ( import (
@@ -1136,7 +1136,7 @@ func main() {
func IsGBK(data []byte) bool func IsGBK(data []byte) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/E2nt3unlmzP)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/E2nt3unlmzP)</span></b>
```go ```go
import ( import (
@@ -1168,7 +1168,7 @@ func main() {
func IsPrintable(str string) bool func IsPrintable(str string) bool
``` ```
<b>Example:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Pe1FE2gdtTP)</span></b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Pe1FE2gdtTP)</span></b>
```go ```go
import ( import (
@@ -1208,7 +1208,7 @@ func main() {
func IsBin(v string) bool func IsBin(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/ogPeg2XJH4P)</span></b>
```go ```go
import ( import (
@@ -1245,7 +1245,7 @@ func main() {
func IsHex(v string) bool func IsHex(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/M2qpHbEwmm7)</span></b>
```go ```go
import ( import (
@@ -1282,7 +1282,7 @@ func main() {
func IsBase64URL(v string) bool func IsBase64URL(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/vhl4mr8GZ6S)</span></b>
```go ```go
import ( import (
@@ -1319,7 +1319,7 @@ func main() {
func IsJWT(v string) bool func IsJWT(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/R6Op7heJbKI)</span></b>
```go ```go
import ( import (
@@ -1350,7 +1350,7 @@ func main() {
func IsVisa(v string) bool func IsVisa(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/SdS2keOyJsl)</span></b>
```go ```go
import ( import (
@@ -1381,7 +1381,7 @@ func main() {
func IsMasterCard(v string) bool func IsMasterCard(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/CwWBFRrG27b)</span></b>
```go ```go
import ( import (
@@ -1412,7 +1412,7 @@ func main() {
func IsAmericanExpress(v string) bool func IsAmericanExpress(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/HIDFpcOdpkd)</span></b>
```go ```go
import ( import (
@@ -1443,7 +1443,7 @@ func main() {
func IsUnionPay(v string) bool func IsUnionPay(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/CUHPEwEITDf)</span></b>
```go ```go
import ( import (
@@ -1474,7 +1474,7 @@ func main() {
func IsChinaUnionPay(v string) bool func IsChinaUnionPay(v string) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/yafpdxLiymu)</span></b>
```go ```go
import ( import (

View File

@@ -1201,7 +1201,7 @@ func Join[T any](slice []T, separator string) string {
} }
// Partition all slice elements with the evaluation of the given predicate functions. // Partition all slice elements with the evaluation of the given predicate functions.
// todo // Play: https://go.dev/play/p/lkQ3Ri2NQhV
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T { func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T {
l := len(predicates) l := len(predicates)

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()

View File

@@ -411,25 +411,25 @@ func IsInt(v any) bool {
} }
// IsBin check if a give string is a valid binary value or not. // IsBin check if a give string is a valid binary value or not.
// Play: todo // Play: https://go.dev/play/p/ogPeg2XJH4P
func IsBin(v string) bool { func IsBin(v string) bool {
return binMatcher.MatchString(v) return binMatcher.MatchString(v)
} }
// IsHex check if a give string is a valid hexadecimal value or not. // IsHex check if a give string is a valid hexadecimal value or not.
// Play: todo // Play: https://go.dev/play/p/M2qpHbEwmm7
func IsHex(v string) bool { func IsHex(v string) bool {
return hexMatcher.MatchString(v) return hexMatcher.MatchString(v)
} }
// IsBase64URL check if a give string is a valid URL-safe Base64 encoded string. // IsBase64URL check if a give string is a valid URL-safe Base64 encoded string.
// Play: todo // Play: https://go.dev/play/p/vhl4mr8GZ6S
func IsBase64URL(v string) bool { func IsBase64URL(v string) bool {
return base64URLMatcher.MatchString(v) return base64URLMatcher.MatchString(v)
} }
// IsJWT check if a give string is is a valid JSON Web Token (JWT). // IsJWT check if a give string is a valid JSON Web Token (JWT).
// Play: todo // Play: https://go.dev/play/p/R6Op7heJbKI
func IsJWT(v string) bool { func IsJWT(v string) bool {
strings := strings.Split(v, ".") strings := strings.Split(v, ".")
if len(strings) != 3 { if len(strings) != 3 {
@@ -446,31 +446,31 @@ func IsJWT(v string) bool {
} }
// IsVisa check if a give string is a valid visa card nubmer or not. // IsVisa check if a give string is a valid visa card nubmer or not.
// Play: todo // Play: https://go.dev/play/p/SdS2keOyJsl
func IsVisa(v string) bool { func IsVisa(v string) bool {
return visaMatcher.MatchString(v) return visaMatcher.MatchString(v)
} }
// IsMasterCard check if a give string is a valid master card nubmer or not. // IsMasterCard check if a give string is a valid master card nubmer or not.
// Play: todo // Play: https://go.dev/play/p/CwWBFRrG27b
func IsMasterCard(v string) bool { func IsMasterCard(v string) bool {
return masterCardMatcher.MatchString(v) return masterCardMatcher.MatchString(v)
} }
// IsAmericanExpress check if a give string is a valid american expression card nubmer or not. // IsAmericanExpress check if a give string is a valid american expression card nubmer or not.
// Play: todo // Play: https://go.dev/play/p/HIDFpcOdpkd
func IsAmericanExpress(v string) bool { func IsAmericanExpress(v string) bool {
return americanExpressMatcher.MatchString(v) return americanExpressMatcher.MatchString(v)
} }
// IsUnionPay check if a give string is a valid union pay nubmer or not. // IsUnionPay check if a give string is a valid union pay nubmer or not.
// Play: todo // Play: https://go.dev/play/p/CUHPEwEITDf
func IsUnionPay(v string) bool { func IsUnionPay(v string) bool {
return unionPay.MatchString(v) return unionPay.MatchString(v)
} }
// IsChinaUnionPay check if a give string is a valid china union pay nubmer or not. // IsChinaUnionPay check if a give string is a valid china union pay nubmer or not.
// Play: todo // Play: https://go.dev/play/p/yafpdxLiymu
func IsChinaUnionPay(v string) bool { func IsChinaUnionPay(v string) bool {
return chinaUnionPay.MatchString(v) return chinaUnionPay.MatchString(v)
} }