mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76b82a2fa1 | ||
|
|
d9dc2993b3 | ||
|
|
ae42a9fdff | ||
|
|
f37e55d9f1 | ||
|
|
d920a51988 | ||
|
|
561c42a24e | ||
|
|
12d74489d5 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -5,6 +5,9 @@ cryptor/*.txt
|
|||||||
fileutil/*.txt
|
fileutil/*.txt
|
||||||
fileutil/*.zip
|
fileutil/*.zip
|
||||||
fileutil/*.link
|
fileutil/*.link
|
||||||
|
fileutil/tempdir
|
||||||
fileutil/unzip/*
|
fileutil/unzip/*
|
||||||
slice/testdata/*
|
slice/testdata/*
|
||||||
cryptor/*.pem
|
cryptor/*.pem
|
||||||
|
docs/node_modules
|
||||||
|
docs/.vitepress
|
||||||
17
README.md
17
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -154,7 +154,9 @@ import "github.com/duke-git/lancet/cryptor"
|
|||||||
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#GenerateRsaKey)
|
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#GenerateRsaKey)
|
||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaDecrypt)
|
||||||
|
- [GenerateRsaKeyPair](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#GenerateRsaKeyPair)
|
||||||
|
- [RsaEncryptOAEP](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaEncryptOAEP)
|
||||||
|
- [RsaDecryptOAEP](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaDecryptOAEP)
|
||||||
### 4. Datetime package supports date and time format and compare.
|
### 4. Datetime package supports date and time format and compare.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -237,6 +239,7 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [WriteCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteCsvFile)
|
- [WriteCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteCsvFile)
|
||||||
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteStringToFile)
|
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteStringToFile)
|
||||||
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteBytesToFile)
|
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteBytesToFile)
|
||||||
|
- [ReadFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadFile)
|
||||||
|
|
||||||
### 6. Formatter contains some functions for data formatting.
|
### 6. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
@@ -510,6 +513,16 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsGBK)
|
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsGBK)
|
||||||
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsASCII)
|
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsASCII)
|
||||||
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsPrintable)
|
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsPrintable)
|
||||||
|
- [IsBin](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsBin)
|
||||||
|
- [IsHex](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsHex)
|
||||||
|
- [IsBase64URL](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsBase64URL)
|
||||||
|
- [IsJWT](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsJWT)
|
||||||
|
- [IsVisa](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsVisa)
|
||||||
|
- [IsMasterCard](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsMasterCard)
|
||||||
|
- [IsAmericanExpress](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsAmericanExpress)
|
||||||
|
- [IsUnionPay](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsUnionPay)
|
||||||
|
- [IsChinaUnionPay](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsChinaUnionPay)
|
||||||
|
|
||||||
|
|
||||||
## How to Contribute
|
## How to Contribute
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -153,6 +153,10 @@ md#HmacSha256WithBase64)
|
|||||||
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#GenerateRsaKey)
|
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#GenerateRsaKey)
|
||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt)
|
||||||
|
- [GenerateRsaKeyPair](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#GenerateRsaKeyPair)
|
||||||
|
- [RsaEncryptOAEP](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncryptOAEP)
|
||||||
|
- [RsaDecryptOAEP](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecryptOAEP)
|
||||||
|
|
||||||
|
|
||||||
### 4. datetime 日期时间处理包,格式化日期,比较日期。
|
### 4. datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
@@ -236,6 +240,7 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [WriteCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteCsvFile)
|
- [WriteCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteCsvFile)
|
||||||
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteStringToFile)
|
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteStringToFile)
|
||||||
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteBytesToFile)
|
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteBytesToFile)
|
||||||
|
- [ReadFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadFile)
|
||||||
|
|
||||||
### 6. formatter 格式化器包含一些数据格式化处理方法。
|
### 6. formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
@@ -509,6 +514,15 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsGBK)
|
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsGBK)
|
||||||
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsASCII)
|
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsASCII)
|
||||||
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsPrintable)
|
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsPrintable)
|
||||||
|
- [IsBin](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsBin)
|
||||||
|
- [IsHex](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsHex)
|
||||||
|
- [IsBase64URL](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsBase64URL)
|
||||||
|
- [IsJWT](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsJWT)
|
||||||
|
- [IsVisa](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsVisa)
|
||||||
|
- [IsMasterCard](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsMasterCard)
|
||||||
|
- [IsAmericanExpress](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsAmericanExpress)
|
||||||
|
- [IsUnionPay](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsUnionPay)
|
||||||
|
- [IsChinaUnionPay](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsChinaUnionPay)
|
||||||
|
|
||||||
## 如何贡献代码
|
## 如何贡献代码
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
@@ -461,3 +462,29 @@ func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
|
|||||||
}
|
}
|
||||||
return plainText
|
return plainText
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateRsaKeyPair create rsa private and public key.
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -128,3 +128,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))
|
||||||
|
}
|
||||||
|
|||||||
115
docs/cryptor.md
115
docs/cryptor.md
@@ -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>
|
||||||
|
|
||||||
@@ -723,6 +726,7 @@ func main() {
|
|||||||
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HmacSha1WithBase64">HmacSha1WithBase64</span>
|
### <span id="HmacSha1WithBase64">HmacSha1WithBase64</span>
|
||||||
|
|
||||||
<p>Return the hmac hash of string use sha1 with base64.</p>
|
<p>Return the hmac hash of string use sha1 with base64.</p>
|
||||||
@@ -1270,3 +1274,114 @@ func main() {
|
|||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <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/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/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/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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -64,6 +64,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>
|
||||||
|
|
||||||
@@ -754,7 +757,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256 hmac值。</p>
|
<p>获取字符串sha256 hmac值。</p>
|
||||||
@@ -883,7 +885,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>获取字符串md5值。</p>
|
<p>获取字符串md5值。</p>
|
||||||
@@ -1301,3 +1302,114 @@ func main() {
|
|||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <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/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/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/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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
@@ -827,3 +828,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
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -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>
|
||||||
|
|
||||||
@@ -827,3 +828,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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -54,7 +54,16 @@ import (
|
|||||||
- [IsZeroValue](#IsZeroValue)
|
- [IsZeroValue](#IsZeroValue)
|
||||||
- [IsGBK](#IsGBK)
|
- [IsGBK](#IsGBK)
|
||||||
- [IsASCII](#IsASCII)
|
- [IsASCII](#IsASCII)
|
||||||
- [IsAIsPrintableSCII](#IsPrintable)
|
- [IsPrintable](#IsPrintable)
|
||||||
|
- [IsBin](#IsBin)
|
||||||
|
- [IsHex](#IsHex)
|
||||||
|
- [IsBase64URL](#IsBase64URL)
|
||||||
|
- [IsJWT](#IsJWT)
|
||||||
|
- [IsVisa](#IsVisa)
|
||||||
|
- [IsMasterCard](#IsMasterCard)
|
||||||
|
- [IsAmericanExpress](#IsAmericanExpress)
|
||||||
|
- [IsUnionPay](#IsUnionPay)
|
||||||
|
- [IsChinaUnionPay](#IsChinaUnionPay)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1026,4 +1035,301 @@ func main() {
|
|||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsBin">IsBin</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid binary value or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBin(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsBin("0101")
|
||||||
|
result2 := validator.IsBin("0b1101")
|
||||||
|
result3 := validator.IsBin("b1101")
|
||||||
|
result4 := validator.IsBin("1201")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsHex">IsHex</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid hexadecimal value or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsHex(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsHex("0xabcde")
|
||||||
|
result2 := validator.IsHex("0XABCDE")
|
||||||
|
result3 := validator.IsHex("cdfeg")
|
||||||
|
result4 := validator.IsHex("0xcdfeg")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsBase64URL">IsBase64URL</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid URL-safe Base64 encoded string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBase64URL(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ")
|
||||||
|
result2 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ==")
|
||||||
|
result3 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ=")
|
||||||
|
result4 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ===")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsJWT">IsJWT</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is is a valid JSON Web Token (JWT).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsJWT(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")
|
||||||
|
result2 := validator.IsJWT("abc")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsVisa">IsVisa</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid visa card nubmer or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsVisa(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsVisa("4111111111111111")
|
||||||
|
result2 := validator.IsVisa("123")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsMasterCard">IsMasterCard</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid mastercard nubmer or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsMasterCard(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsMasterCard("5425233430109903")
|
||||||
|
result2 := validator.IsMasterCard("4111111111111111")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsAmericanExpress">IsAmericanExpress</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid american express nubmer or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsAmericanExpress(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsAmericanExpress("342883359122187")
|
||||||
|
result2 := validator.IsAmericanExpress("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsUnionPay">IsVisa</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid union pay nubmer or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsUnionPay(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsUnionPay("6221263430109903")
|
||||||
|
result2 := validator.IsUnionPay("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsChinaUnionPay">IsChinaUnionPay</span>
|
||||||
|
|
||||||
|
<p>Checks if a give string is a valid china union pay nubmer or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsChinaUnionPay(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsChinaUnionPay("6250941006528599")
|
||||||
|
result2 := validator.IsChinaUnionPay("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -55,6 +55,15 @@ import (
|
|||||||
- [IsGBK](#IsGBK)
|
- [IsGBK](#IsGBK)
|
||||||
- [IsASCII](#IsASCII)
|
- [IsASCII](#IsASCII)
|
||||||
- [IsPrintable](#IsPrintable)
|
- [IsPrintable](#IsPrintable)
|
||||||
|
- [IsBin](#IsBin)
|
||||||
|
- [IsHex](#IsHex)
|
||||||
|
- [IsBase64URL](#IsBase64URL)
|
||||||
|
- [IsJWT](#IsJWT)
|
||||||
|
- [IsVisa](#IsVisa)
|
||||||
|
- [IsMasterCard](#IsMasterCard)
|
||||||
|
- [IsAmericanExpress](#IsAmericanExpress)
|
||||||
|
- [IsUnionPay](#IsUnionPay)
|
||||||
|
- [IsChinaUnionPay](#IsChinaUnionPay)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -958,6 +967,7 @@ func main() {
|
|||||||
fmt.Println("data encoding is unknown")
|
fmt.Println("data encoding is unknown")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsASCII">IsASCII</span>
|
### <span id="IsASCII">IsASCII</span>
|
||||||
|
|
||||||
<p>验证字符串全部为ASCII字符。</p>
|
<p>验证字符串全部为ASCII字符。</p>
|
||||||
@@ -1036,4 +1046,301 @@ func main() {
|
|||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsBin">IsBin</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的二进制数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBin(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsBin("0101")
|
||||||
|
result2 := validator.IsBin("0b1101")
|
||||||
|
result3 := validator.IsBin("b1101")
|
||||||
|
result4 := validator.IsBin("1201")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsHex">IsHex</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的十六进制数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsHex(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsHex("0xabcde")
|
||||||
|
result2 := validator.IsHex("0XABCDE")
|
||||||
|
result3 := validator.IsHex("cdfeg")
|
||||||
|
result4 := validator.IsHex("0xcdfeg")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsBase64URL">IsBase64URL</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的base64 url。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBase64URL(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ")
|
||||||
|
result2 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ==")
|
||||||
|
result3 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ=")
|
||||||
|
result4 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ===")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsJWT">IsJWT</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的JSON Web Token (JWT)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsJWT(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")
|
||||||
|
result2 := validator.IsJWT("abc")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsVisa">IsVisa</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的visa卡号。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsVisa(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsVisa("4111111111111111")
|
||||||
|
result2 := validator.IsVisa("123")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsMasterCard">IsMasterCard</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的MasterCard卡号。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsMasterCard(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsMasterCard("5425233430109903")
|
||||||
|
result2 := validator.IsMasterCard("4111111111111111")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsAmericanExpress">IsAmericanExpress</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的American Express卡号。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsAmericanExpress(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsAmericanExpress("342883359122187")
|
||||||
|
result2 := validator.IsAmericanExpress("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsUnionPay">IsUnionPay</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的美国银联卡号。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsUnionPay(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsUnionPay("6221263430109903")
|
||||||
|
result2 := validator.IsUnionPay("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsChinaUnionPay">IsChinaUnionPay</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否是有效的中国银联卡号。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsChinaUnionPay(v string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsChinaUnionPay("6250941006528599")
|
||||||
|
result2 := validator.IsChinaUnionPay("3782822463100007")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
164
fileutil/file.go
164
fileutil/file.go
@@ -19,10 +19,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsExist checks if a file or directory exists
|
// IsExist checks if a file or directory exists
|
||||||
@@ -168,7 +169,15 @@ func ListFileNames(path string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Zip create zip file, fpath could be a single file or a directory
|
// Zip create zip file, fpath could be a single file or a directory
|
||||||
func Zip(fpath string, destPath string) error {
|
func Zip(path string, destPath string) error {
|
||||||
|
if IsDir(path) {
|
||||||
|
return zipFolder(path, destPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return zipFile(path, destPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func zipFile(filePath string, destPath string) error {
|
||||||
zipFile, err := os.Create(destPath)
|
zipFile, err := os.Create(destPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -178,7 +187,97 @@ func Zip(fpath string, destPath string) error {
|
|||||||
archive := zip.NewWriter(zipFile)
|
archive := zip.NewWriter(zipFile)
|
||||||
defer archive.Close()
|
defer archive.Close()
|
||||||
|
|
||||||
return addFileToArchive(fpath, archive)
|
return addFileToArchive1(filePath, archive)
|
||||||
|
}
|
||||||
|
|
||||||
|
func zipFolder(folderPath string, destPath string) error {
|
||||||
|
outFile, err := os.Create(destPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer outFile.Close()
|
||||||
|
|
||||||
|
w := zip.NewWriter(outFile)
|
||||||
|
|
||||||
|
err = addFileToArchive2(w, folderPath, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFileToArchive1(fpath string, archive *zip.Writer) error {
|
||||||
|
err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
header, err := zip.FileInfoHeader(info)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/")
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
header.Name += "/"
|
||||||
|
} else {
|
||||||
|
header.Method = zip.Deflate
|
||||||
|
writer, err := archive.CreateHeader(header)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
if _, err := io.Copy(writer, file); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFileToArchive2(w *zip.Writer, basePath, baseInZip string) error {
|
||||||
|
files, err := os.ReadDir(basePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(basePath, "/") {
|
||||||
|
basePath = basePath + "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if !file.IsDir() {
|
||||||
|
dat, err := os.ReadFile(basePath + file.Name())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := w.Create(baseInZip + file.Name())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = f.Write(dat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if file.IsDir() {
|
||||||
|
newBase := basePath + file.Name() + "/"
|
||||||
|
addFileToArchive2(w, newBase, baseInZip+file.Name()+"/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnZip unzip the file and save it to destPath
|
// UnZip unzip the file and save it to destPath
|
||||||
@@ -259,7 +358,7 @@ func ZipAppendEntry(fpath string, destPath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = addFileToArchive(fpath, archive)
|
err = addFileToArchive1(fpath, archive)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -281,41 +380,6 @@ func ZipAppendEntry(fpath string, destPath string) error {
|
|||||||
return CopyFile(tempFile.Name(), destPath)
|
return CopyFile(tempFile.Name(), destPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addFileToArchive(fpath string, archive *zip.Writer) error {
|
|
||||||
err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
header, err := zip.FileInfoHeader(info)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/")
|
|
||||||
|
|
||||||
if info.IsDir() {
|
|
||||||
header.Name += "/"
|
|
||||||
} else {
|
|
||||||
header.Method = zip.Deflate
|
|
||||||
writer, err := archive.CreateHeader(header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
if _, err := io.Copy(writer, file); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func safeFilepathJoin(path1, path2 string) (string, error) {
|
func safeFilepathJoin(path1, path2 string) (string, error) {
|
||||||
relPath, err := filepath.Rel(".", path2)
|
relPath, err := filepath.Rel(".", path2)
|
||||||
if err != nil || strings.HasPrefix(relPath, "..") {
|
if err != nil || strings.HasPrefix(relPath, "..") {
|
||||||
@@ -386,7 +450,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
|
||||||
@@ -526,3 +590,23 @@ 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.
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package fileutil
|
package fileutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -294,7 +295,11 @@ func TestSha(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSha")
|
assert := internal.NewAssert(t, "TestSha")
|
||||||
|
|
||||||
sha1, err := Sha("./testdata/test.txt", 1)
|
sha1, err := Sha("./testdata/test.txt", 1)
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
sha256, err := Sha("./testdata/test.txt", 256)
|
sha256, err := Sha("./testdata/test.txt", 256)
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
sha512, err := Sha("./testdata/test.txt", 512)
|
sha512, err := Sha("./testdata/test.txt", 512)
|
||||||
|
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
@@ -401,3 +406,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))
|
||||||
|
}
|
||||||
|
|||||||
1
fileutil/testdata/test1.csv
vendored
1
fileutil/testdata/test1.csv
vendored
@@ -1,2 +1,3 @@
|
|||||||
Lili,22,female
|
Lili,22,female
|
||||||
Jim,21,male
|
Jim,21,male
|
||||||
|
|
||||||
|
|||||||
|
@@ -16,18 +16,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||||
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
||||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
||||||
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||||
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
||||||
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||||
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
||||||
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
||||||
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
||||||
|
base64URLMatcher *regexp.Regexp = regexp.MustCompile(`^([A-Za-z0-9_-]{4})*([A-Za-z0-9_-]{2}(==)?|[A-Za-z0-9_-]{3}=?)?$`)
|
||||||
|
binMatcher *regexp.Regexp = regexp.MustCompile(`^(0b)?[01]+$`)
|
||||||
|
hexMatcher *regexp.Regexp = regexp.MustCompile(`^(#|0x|0X)?[0-9a-fA-F]+$`)
|
||||||
|
visaMatcher *regexp.Regexp = regexp.MustCompile(`^4[0-9]{12}(?:[0-9]{3})?$`)
|
||||||
|
masterCardMatcher *regexp.Regexp = regexp.MustCompile(`^5[1-5][0-9]{14}$`)
|
||||||
|
americanExpressMatcher *regexp.Regexp = regexp.MustCompile(`^3[47][0-9]{13}$`)
|
||||||
|
unionPay *regexp.Regexp = regexp.MustCompile("^62[0-5]\\d{13,16}$")
|
||||||
|
chinaUnionPay *regexp.Regexp = regexp.MustCompile(`^62[0-9]{14,17}$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
||||||
@@ -362,3 +370,59 @@ func IsPrintable(str string) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsBin check if a give string is a valid binary value or not.
|
||||||
|
func IsBin(v string) bool {
|
||||||
|
return binMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsHex check if a give string is a valid hexadecimal value or not.
|
||||||
|
func IsHex(v string) bool {
|
||||||
|
return hexMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsBase64URL check if a give string is a valid URL-safe Base64 encoded string.
|
||||||
|
func IsBase64URL(v string) bool {
|
||||||
|
return base64URLMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsJWT check if a give string is a valid JSON Web Token (JWT).
|
||||||
|
func IsJWT(v string) bool {
|
||||||
|
strings := strings.Split(v, ".")
|
||||||
|
if len(strings) != 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range strings {
|
||||||
|
if !IsBase64URL(s) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsVisa check if a give string is a valid visa card nubmer or not.
|
||||||
|
func IsVisa(v string) bool {
|
||||||
|
return visaMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMasterCard check if a give string is a valid master card nubmer or not.
|
||||||
|
func IsMasterCard(v string) bool {
|
||||||
|
return masterCardMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsAmericanExpress check if a give string is a valid american expression card nubmer or not.
|
||||||
|
func IsAmericanExpress(v string) bool {
|
||||||
|
return americanExpressMatcher.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsUnionPay check if a give string is a valid union pay nubmer or not.
|
||||||
|
func IsUnionPay(v string) bool {
|
||||||
|
return unionPay.MatchString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsChinaUnionPay check if a give string is a valid china union pay nubmer or not.
|
||||||
|
func IsChinaUnionPay(v string) bool {
|
||||||
|
return chinaUnionPay.MatchString(v)
|
||||||
|
}
|
||||||
|
|||||||
@@ -448,3 +448,90 @@ func TestIsPrintable(t *testing.T) {
|
|||||||
assert.Equal(true, IsPrintable("😄"))
|
assert.Equal(true, IsPrintable("😄"))
|
||||||
assert.Equal(false, IsPrintable("\u0000"))
|
assert.Equal(false, IsPrintable("\u0000"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsBin(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsBin")
|
||||||
|
|
||||||
|
assert.Equal(true, IsBin("0101"))
|
||||||
|
assert.Equal(true, IsBin("0b1101"))
|
||||||
|
|
||||||
|
assert.Equal(false, IsBin("b1101"))
|
||||||
|
assert.Equal(false, IsBin("1201"))
|
||||||
|
assert.Equal(false, IsBin(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsHex(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsHex")
|
||||||
|
|
||||||
|
assert.Equal(true, IsHex("ABCDE"))
|
||||||
|
assert.Equal(true, IsHex("abcde"))
|
||||||
|
assert.Equal(true, IsHex("0xabcde"))
|
||||||
|
assert.Equal(true, IsHex("0Xabcde"))
|
||||||
|
assert.Equal(true, IsHex("#abcde"))
|
||||||
|
|
||||||
|
assert.Equal(false, IsHex("cdfeg"))
|
||||||
|
assert.Equal(false, IsHex("0xcdfeg"))
|
||||||
|
assert.Equal(false, IsHex(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsBase64URL(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsBase64URL")
|
||||||
|
|
||||||
|
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ"))
|
||||||
|
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ=="))
|
||||||
|
|
||||||
|
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ="))
|
||||||
|
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ==="))
|
||||||
|
// assert.Equal(false, IsBase64URL(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsJWT(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsJWT")
|
||||||
|
|
||||||
|
assert.Equal(true, IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910"))
|
||||||
|
assert.Equal(false, IsJWT("abc"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsVisa(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsVisa")
|
||||||
|
|
||||||
|
assert.Equal(true, IsVisa("4111111111111111"))
|
||||||
|
assert.Equal(false, IsVisa("123"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsMasterCard(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsMasterCard")
|
||||||
|
|
||||||
|
assert.Equal(true, IsMasterCard("5425233430109903"))
|
||||||
|
assert.Equal(false, IsMasterCard("4111111111111111"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsAmericanExpress(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsAmericanExpress")
|
||||||
|
|
||||||
|
assert.Equal(true, IsAmericanExpress("342883359122187"))
|
||||||
|
assert.Equal(false, IsAmericanExpress("3782822463100007"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsUnionPay(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsUnionPay")
|
||||||
|
|
||||||
|
assert.Equal(true, IsUnionPay("6221263430109903"))
|
||||||
|
assert.Equal(false, IsUnionPay("3782822463100007"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsChinaUnionPay(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestIsChinaUnionPay")
|
||||||
|
|
||||||
|
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
||||||
|
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user