Compare commits
2 Commits
9f11e1a05d
...
bfe97d5184
Author | SHA1 | Date |
---|---|---|
hzy | bfe97d5184 | 2 years ago |
hzy | ec7e8a4d41 | 2 years ago |
@ -0,0 +1,108 @@ |
||||
# ---> JetBrains |
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm |
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 |
||||
|
||||
# User-specific stuff |
||||
.idea/**/workspace.xml |
||||
.idea/**/tasks.xml |
||||
.idea/**/usage.statistics.xml |
||||
.idea/**/dictionaries |
||||
.idea/**/shelf |
||||
|
||||
# Generated files |
||||
.idea/**/contentModel.xml |
||||
|
||||
# Sensitive or high-churn files |
||||
.idea/**/dataSources/ |
||||
.idea/**/dataSources.ids |
||||
.idea/**/dataSources.local.xml |
||||
.idea/**/sqlDataSources.xml |
||||
.idea/**/dynamic.xml |
||||
.idea/**/uiDesigner.xml |
||||
.idea/**/dbnavigator.xml |
||||
|
||||
# Gradle |
||||
.idea/**/gradle.xml |
||||
.idea/**/libraries |
||||
|
||||
# Gradle and Maven with auto-import |
||||
# When using Gradle or Maven with auto-import, you should exclude module files, |
||||
# since they will be recreated, and may cause churn. Uncomment if using |
||||
# auto-import. |
||||
# .idea/artifacts |
||||
# .idea/compiler.xml |
||||
# .idea/jarRepositories.xml |
||||
# .idea/modules.xml |
||||
# .idea/*.iml |
||||
# .idea/modules |
||||
# *.iml |
||||
# *.ipr |
||||
|
||||
# CMake |
||||
cmake-build-*/ |
||||
|
||||
# Mongo Explorer plugin |
||||
.idea/**/mongoSettings.xml |
||||
|
||||
# File-based project format |
||||
*.iws |
||||
|
||||
# IntelliJ |
||||
out/ |
||||
|
||||
# mpeltonen/sbt-idea plugin |
||||
.idea_modules/ |
||||
|
||||
# JIRA plugin |
||||
atlassian-ide-plugin.xml |
||||
|
||||
# Cursive Clojure plugin |
||||
.idea/replstate.xml |
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ) |
||||
com_crashlytics_export_strings.xml |
||||
crashlytics.properties |
||||
crashlytics-build.properties |
||||
fabric.properties |
||||
|
||||
# Editor-based Rest Client |
||||
.idea/httpRequests |
||||
|
||||
# Android studio 3.1+ serialized cache file |
||||
.idea/caches/build_file_checksums.ser |
||||
|
||||
# Reference https://github.com/github/gitignore/blob/master/Go.gitignore |
||||
# Binaries for programs and plugins |
||||
*.exe |
||||
*.exe~ |
||||
*.dll |
||||
*.so |
||||
*.dylib |
||||
|
||||
# Test binary, built with `go test -c` |
||||
*.test |
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE |
||||
*.out |
||||
|
||||
# Dependency directories (remove the comment below to include it) |
||||
vendor/ |
||||
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects) |
||||
*.o |
||||
*.a |
||||
|
||||
# OS General |
||||
Thumbs.db |
||||
.DS_Store |
||||
|
||||
# project |
||||
*.cert |
||||
*.key |
||||
*.log |
||||
bin/ |
||||
|
||||
# Develop tools |
||||
.vscode/ |
||||
.idea/ |
||||
*.swp |
@ -1,3 +1,58 @@ |
||||
# utils |
||||
# utils通用包 |
||||
|
||||
通用包 |
||||
<!-- TOC --> |
||||
* [utils通用包](#utils通用包) |
||||
* [安装](#安装) |
||||
* [通用包](#通用包) |
||||
* [pagination](#pagination) |
||||
* [transport:通用http ResponseEncoder](#transport通用http-responseencoder) |
||||
* [meta:跨服务meta信息设置和获取](#meta跨服务meta信息设置和获取) |
||||
* [encrypt:对称加密](#encrypt对称加密) |
||||
<!-- TOC --> |
||||
|
||||
## 安装 |
||||
|
||||
1. 设置私有仓库 |
||||
|
||||
```shell |
||||
go env -w GOPRIVATE=gitea.drugeyes.vip |
||||
``` |
||||
|
||||
2. 设置git |
||||
|
||||
```shell |
||||
git config --global url."ssh://gitea@gitea.drugeyes.vip".insteadOf "https://gitea.drugeyes.vip" |
||||
``` |
||||
|
||||
3. 拉取对应版本 |
||||
|
||||
```shell |
||||
go get gitea.drugeyes.vip/pharnexbase/tools@v1.0.0 |
||||
``` |
||||
|
||||
## 通用包 |
||||
|
||||
### pagination |
||||
|
||||
* v1 |
||||
|
||||
> 说明 |
||||
|
||||
### transport:通用http ResponseEncoder |
||||
|
||||
* v1 |
||||
|
||||
> 通用http ResponseEncoder |
||||
|
||||
### meta:跨服务meta信息设置和获取 |
||||
|
||||
* v1 |
||||
|
||||
> 跨服务meta信息设置和获取 |
||||
> - user(基础用户信息) |
||||
|
||||
### encrypt:对称加密 |
||||
|
||||
* v1 |
||||
|
||||
> 对称加密,目前包含:SHA1PRNG |
@ -0,0 +1,102 @@ |
||||
package encrypt |
||||
|
||||
import ( |
||||
"crypto/aes" |
||||
"crypto/hmac" |
||||
"crypto/sha1" |
||||
"errors" |
||||
) |
||||
|
||||
//AesEncryptECBSha1prng java AES 加密 SHA1PRNG
|
||||
func AesEncryptECBSha1prng(src []byte, key []byte) ([]byte, error) { |
||||
sha1prngKey, err := GetSha1prngKey(key, 128) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return AesEncryptECB(src, sha1prngKey), nil |
||||
} |
||||
|
||||
//AesDecryptECBSha1prng java AES 解密 SHA1PRNG
|
||||
func AesDecryptECBSha1prng(encrypted []byte, key []byte) ([]byte, error) { |
||||
sha1prngKey, err := GetSha1prngKey(key, 128) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return AesDecryptECB(encrypted, sha1prngKey), nil |
||||
} |
||||
|
||||
//AesEncryptECB AES-128-ECB 加密,(php:openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA))
|
||||
func AesEncryptECB(src []byte, key []byte) []byte { |
||||
c, _ := aes.NewCipher(generateKey(key)) |
||||
length := (len(src) + aes.BlockSize) / aes.BlockSize |
||||
plain := make([]byte, length*aes.BlockSize) |
||||
copy(plain, src) |
||||
pad := byte(len(plain) - len(src)) |
||||
for i := len(src); i < len(plain); i++ { |
||||
plain[i] = pad |
||||
} |
||||
encrypted := make([]byte, len(plain)) |
||||
// 分组分块加密
|
||||
for bs, be := 0, c.BlockSize(); bs <= len(src); bs, be = bs+c.BlockSize(), be+c.BlockSize() { |
||||
c.Encrypt(encrypted[bs:be], plain[bs:be]) |
||||
} |
||||
|
||||
return encrypted |
||||
} |
||||
|
||||
//AesDecryptECB AES-128-ECB 解密
|
||||
func AesDecryptECB(encrypted []byte, key []byte) []byte { |
||||
c, _ := aes.NewCipher(generateKey(key)) |
||||
decrypted := make([]byte, len(encrypted)) |
||||
|
||||
for bs, be := 0, c.BlockSize(); bs < len(encrypted); bs, be = bs+c.BlockSize(), be+c.BlockSize() { |
||||
c.Decrypt(decrypted[bs:be], encrypted[bs:be]) |
||||
} |
||||
|
||||
trim := 0 |
||||
if len(decrypted) > 0 { |
||||
trim = len(decrypted) - int(decrypted[len(decrypted)-1]) |
||||
} |
||||
|
||||
return decrypted[:trim] |
||||
} |
||||
|
||||
// GetSha1prngKey 模拟 java SHA1PRNG 处理,(php:substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16))
|
||||
func GetSha1prngKey(keyBytes []byte, encryptLength int) ([]byte, error) { |
||||
hashs := Sha1(Sha1(keyBytes)) |
||||
maxLen := len(hashs) |
||||
realLen := encryptLength / 8 |
||||
if realLen > maxLen { |
||||
return nil, errors.New("invalid length") |
||||
} |
||||
|
||||
return hashs[0:realLen], nil |
||||
} |
||||
|
||||
//Sha1 Sha1
|
||||
func Sha1(data []byte) []byte { |
||||
h := sha1.New() |
||||
h.Write(data) |
||||
return h.Sum(nil) |
||||
} |
||||
|
||||
// HmacSha1 HmacSha1
|
||||
func HmacSha1(string, secret string) []byte { |
||||
mac := hmac.New(sha1.New, []byte(secret)) |
||||
mac.Write([]byte(string)) |
||||
|
||||
return mac.Sum(nil) |
||||
} |
||||
|
||||
func generateKey(key []byte) (genKey []byte) { |
||||
genKey = make([]byte, 16) |
||||
copy(genKey, key) |
||||
for i := 16; i < len(key); { |
||||
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 { |
||||
genKey[j] ^= key[i] |
||||
} |
||||
} |
||||
return genKey |
||||
} |
@ -0,0 +1,75 @@ |
||||
package meta |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"errors" |
||||
"github.com/go-kratos/kratos/v2/metadata" |
||||
) |
||||
|
||||
const ( |
||||
userMetaKey = "x-md-global-user" |
||||
) |
||||
|
||||
var UserErr = errors.New("userinfo error") |
||||
|
||||
// User 用户信息
|
||||
type User struct { |
||||
// 用户id
|
||||
Id int32 `json:"id"` |
||||
// 邮箱
|
||||
Email string `json:"email"` |
||||
// 账号
|
||||
Username string `json:"username"` |
||||
// 手机号
|
||||
Phone string `json:"phone"` |
||||
// 昵称
|
||||
Nickname string `json:"nickname"` |
||||
// 头像
|
||||
Avatar string `json:"avatar"` |
||||
// 状态 1=正常;2=封禁
|
||||
Status int32 `json:"status"` |
||||
} |
||||
|
||||
// SetUserMetaClient 设置用户meta信息
|
||||
func SetUserMetaClient(ctx context.Context, user *User) context.Context { |
||||
userInfo, _ := json.Marshal(user) |
||||
return metadata.AppendToClientContext(ctx, userMetaKey, string(userInfo)) |
||||
} |
||||
|
||||
// GetUserMetaClient 获取用户meta信息
|
||||
func GetUserMetaClient(ctx context.Context) (*User, error) { |
||||
if meta, ok := metadata.FromClientContext(ctx); ok { |
||||
user := &User{} |
||||
if json.Unmarshal([]byte(meta.Get(userMetaKey)), user) != nil { |
||||
return nil, UserErr |
||||
} |
||||
return user, nil |
||||
} |
||||
return nil, UserErr |
||||
} |
||||
|
||||
// SetUserMetaServer 设置用户meta信息
|
||||
func SetUserMetaServer(ctx context.Context, user *User) context.Context { |
||||
userInfo, _ := json.Marshal(user) |
||||
|
||||
var meta metadata.Metadata |
||||
var ok bool |
||||
if meta, ok = metadata.FromServerContext(ctx); ok { |
||||
meta.Set(userMetaKey, string(userInfo)) |
||||
} |
||||
|
||||
return metadata.NewServerContext(ctx, meta) |
||||
} |
||||
|
||||
// GetUserMetaServer 获取用户meta信息
|
||||
func GetUserMetaServer(ctx context.Context) (*User, error) { |
||||
if meta, ok := metadata.FromServerContext(ctx); ok { |
||||
user := &User{} |
||||
if json.Unmarshal([]byte(meta.Get(userMetaKey)), user) != nil { |
||||
return nil, UserErr |
||||
} |
||||
return user, nil |
||||
} |
||||
return nil, UserErr |
||||
} |
Loading…
Reference in new issue