diff --git a/api/consts/status.go b/api/consts/status.go index 2300f9ca..06f03c66 100644 --- a/api/consts/status.go +++ b/api/consts/status.go @@ -247,3 +247,9 @@ const ( IsCreatingPaidText_No = 0 //否 IsCreatingPaidText_Yes = 1 //是 ) + +const ( + HygAgreeState_Fail = "-1" // 签约失败 + HygAgreeState_Waiting = "1" // 待生成电子签 + HygAgreeState_Success = "2" // 签约成功 +) diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index 2165e353..7ecd0142 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -211,6 +211,9 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeDailyStatementZoneInfoSrvFail: "空间相关每日报表服务错误", ErrCodeDailyStatementZoneInfoNotExist: "空间相关每日报表不存在", + + ErrCodeWorkerIdSrvFail: "用户职业者id映射表服务错误", + ErrCodeWorkerIdNotExist: "用户职业者id映射表不存在", } const ( @@ -505,6 +508,11 @@ const ( ErrCodeDailyStatementZoneInfoSrvFail ErrCode = -40001 // 空间相关每日报表服务错误 ErrCodeDailyStatementZoneInfoNotExist ErrCode = -40002 // 空间相关每日报表不存在 + // WorkerId: 41xxx + ErrCodeWorkerIdSrvOk ErrCode = ErrCodeOk + ErrCodeWorkerIdSrvFail ErrCode = -41001 // 用户职业者id映射表服务错误 + ErrCodeWorkerIdNotExist ErrCode = -41002 // 用户职业者id映射表不存在 + // Media: 60xxx ErrCodeMediaSrvOk ErrCode = ErrCodeOk ErrCodeMediaSrvFail ErrCode = -60001 // 媒体服务错误 diff --git a/api/proto/hvyogo/proto/hvyogo_ext.go b/api/proto/hvyogo/proto/hvyogo_ext.go new file mode 100644 index 00000000..aa0ede1d --- /dev/null +++ b/api/proto/hvyogo/proto/hvyogo_ext.go @@ -0,0 +1,6 @@ +package proto + +type HvyogoCallbackReq struct { + CooperatorId string `json:"cooperatorId"` // 商户对接唯一标识,手机号 + BusinessBody string `json:"businessBody" jcrypto:"hyg_aes"` // AES加密后的字符串 +} diff --git a/api/proto/hvyogo/proto/hvyogo_vo.go b/api/proto/hvyogo/proto/hvyogo_vo.go new file mode 100644 index 00000000..8309a420 --- /dev/null +++ b/api/proto/hvyogo/proto/hvyogo_vo.go @@ -0,0 +1,8 @@ +package proto + +type HvyogoVO struct { + WorkerId string `json:"workerId"` // 职业者id + AgreeState string `json:"agreeState"` // 签约状态 + AgreeDesc string `json:"agreeDesc"` // 签约状态描述 + WorkerMobile string `json:"workerMobile"` // 自由职业者手机号 +} diff --git a/api/proto/worker_id/proto/worker_id_op.go b/api/proto/worker_id/proto/worker_id_op.go new file mode 100644 index 00000000..a95e8ada --- /dev/null +++ b/api/proto/worker_id/proto/worker_id_op.go @@ -0,0 +1,65 @@ +package proto + +import ( + "service/api/base" + "service/dbstruct" +) + +// op 创建 +type OpCreateReq struct { + base.BaseRequest + *dbstruct.WorkerId +} + +type OpCreateData struct { +} + +type OpCreateResp struct { + base.BaseResponse + Data *OpCreateData `json:"data"` +} + +// op 删除 +type OpDeleteReq struct { + base.BaseRequest + Id int64 `json:"id"` +} + +type OpDeleteData struct { +} + +type OpDeleteResp struct { + base.BaseResponse + Data *OpDeleteData `json:"data"` +} + +// op 更新 +type OpUpdateReq struct { + base.BaseRequest + *dbstruct.WorkerId +} + +type OpUpdateData struct { +} + +type OpUpdateResp struct { + base.BaseResponse + Data *OpUpdateData `json:"data"` +} + +// op 列表 +type OpListByMidReq struct { + base.BaseRequest + Mid int64 `json:"mid"` +} + +type OpListByMidData struct { + WorkerId *dbstruct.WorkerId `json:"worker_id"` + Offset int `json:"offset"` + More int `json:"more"` +} + +type OpListByMidResp struct { + base.BaseResponse + Data *OpListByMidData `json:"data"` +} diff --git a/app/mix/controller/hvyogo_callback.go b/app/mix/controller/hvyogo_callback.go new file mode 100644 index 00000000..1a0e7e25 --- /dev/null +++ b/app/mix/controller/hvyogo_callback.go @@ -0,0 +1,27 @@ +package controller + +import ( + "service/api/errcode" + "service/api/message/response" + hvyogoproto "service/api/proto/hvyogo/proto" + "service/app/mix/service" + "service/bizcommon/util" + "service/library/logger" + + "github.com/gin-gonic/gin" +) + +func HvyogoCallback(ctx *gin.Context) { + + req := ctx.MustGet("client_req").(*hvyogoproto.HvyogoCallbackReq) + + // 存入数据 + ec := service.DefaultService.SaveHvyogoCallback(ctx, req) + if ec != errcode.ErrCodeWorkerIdSrvOk { + logger.Error("SaveHvyogoCallback fail, req: %v, ec: %v", util.ToJson(req), ec) + response.ReplyErrCodeMsg(ctx, ec) + return + } + + response.ReplyOk(ctx, nil) +} diff --git a/app/mix/controller/init.go b/app/mix/controller/init.go index 1ea583b6..99ba12e5 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -31,6 +31,7 @@ import ( daily_statement_zone_info_proto "service/api/proto/daily_statement_zone_info/proto" feedbackproto "service/api/proto/feedback/proto" footprintproto "service/api/proto/footprint/proto" + hvyogoproto "service/api/proto/hvyogo/proto" loginproto "service/api/proto/login/proto" momentproto "service/api/proto/moment/proto" moment_audit_taskproto "service/api/proto/moment_audit_task/proto" @@ -306,6 +307,9 @@ func Init(r *gin.Engine) { opVasPayGroup.POST("zone_refund_list", middleware.JSONParamValidator(zoneproto.OpZoneRefundListParam{}), OpZoneRefundList) opVasPayGroup.POST("manual_unlock_wechat", middleware.JSONParamValidator(zoneproto.OpManualUnlockWechatParam{}), OpManualUnlockWechat) + extEsbGroup := r.Group("/ext/hvyogo") + extEsbGroup.POST("agree_callback", middleware.FORMParamValidator(hvyogoproto.HvyogoCallbackReq{}), middleware.RequestDecryptor(), HvyogoCallback) + // 验证码 opVeriCodeGroup := r.Group("/op/veri_code", PrepareOp()) opVeriCodeGroup.POST("send", middleware.JSONParamValidator(vericodeproto.OpSendReq{}), middleware.RequestDecryptor(), OpSendVeriCode) diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 3fd64a9a..6bf538e2 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -42,6 +42,7 @@ import ( userwxaddcheckproto "service/api/proto/userwxaddcheck/proto" vericodeproto "service/api/proto/vericode/proto" video_moderation_task_proto "service/api/proto/video_moderation_task/proto" + workeridproto "service/api/proto/worker_id/proto" zoneproto "service/api/proto/zone/proto" zone_collaborator_proto "service/api/proto/zone_collaborator/proto" zone_third_partner_proto "service/api/proto/zone_third_partner/proto" @@ -213,6 +214,9 @@ const ( DBStreamerScore = "streamer_score" COLStreamerScore = "streamer_score" + + DBWorkerId = "worker_id" + COLWorkerId = "worker_id" ) // 商品表 @@ -539,6 +543,11 @@ func (m *Mongo) getColStreamerScore() *qmgo.Collection { return m.clientMix.Database(DBStreamerScore).Collection(COLStreamerScore) } +// 用户职业者id映射表表 +func (m *Mongo) getColWorkerId() *qmgo.Collection { + return m.clientMix.Database(DBWorkerId).Collection(COLWorkerId) +} + // 商品相关 func (m *Mongo) CreateProduct(ctx *gin.Context, product *dbstruct.Product) error { col := m.getColProduct() @@ -5217,3 +5226,47 @@ func (m *Mongo) SetStreamerScore(ctx *gin.Context, list []*dbstruct.StreamerScor } return err } + +// 用户职业者id映射表相关 +func (m *Mongo) CreateWorkerId(ctx *gin.Context, worker_id *dbstruct.WorkerId) error { + col := m.getColWorkerId() + _, err := col.InsertOne(ctx, worker_id) + return err +} + +func (m *Mongo) UpdateWorkerId(ctx *gin.Context, worker_id *dbstruct.WorkerId) error { + col := m.getColWorkerId() + set := util.EntityToM(worker_id) + set["ut"] = time.Now().Unix() + up := qmgo.M{ + "$set": set, + } + err := col.UpdateId(ctx, worker_id.Id, up) + return err +} + +func (m *Mongo) DeleteWorkerId(ctx *gin.Context, id int64) error { + col := m.getColWorkerId() + update := qmgo.M{ + "$set": qmgo.M{ + "del_flag": 1, + }, + } + err := col.UpdateId(ctx, id, update) + return err +} + +func (m *Mongo) GetWorkerIdByMid(ctx *gin.Context, req *workeridproto.OpListByMidReq) (*dbstruct.WorkerId, error) { + workerId := &dbstruct.WorkerId{} + col := m.getColWorkerId() + query := qmgo.M{ + "mid": req.Mid, + "del_flag": 0, + } + err := col.Find(ctx, query).One(workerId) + if err == qmgo.ErrNoSuchDocuments { + err = nil + return nil, err + } + return workerId, err +} diff --git a/app/mix/service/logic/worker_id.go b/app/mix/service/logic/worker_id.go new file mode 100644 index 00000000..4dd53019 --- /dev/null +++ b/app/mix/service/logic/worker_id.go @@ -0,0 +1,65 @@ +package logic + +import ( + "service/api/consts" + worker_idproto "service/api/proto/worker_id/proto" + "service/app/mix/dao" + "service/dbstruct" + "service/library/idgenerator" + "service/library/logger" + "time" + + "github.com/gin-gonic/gin" + goproto "google.golang.org/protobuf/proto" +) + +type WorkerId struct { + store *dao.Store +} + +func NewWorkerId(store *dao.Store) (a *WorkerId) { + a = &WorkerId{ + store: store, + } + return +} + +func (p *WorkerId) OpCreate(ctx *gin.Context, req *worker_idproto.OpCreateReq) error { + req.WorkerId.Id = goproto.Int64(idgenerator.GenWorkerIdId()) + req.WorkerId.Ct = goproto.Int64(time.Now().Unix()) + req.WorkerId.Ut = goproto.Int64(time.Now().Unix()) + req.WorkerId.DelFlag = goproto.Int64(consts.Exist) + err := p.store.CreateWorkerId(ctx, req.WorkerId) + if err != nil { + logger.Error("CreateWorkerId fail, err: %v", err) + return err + } + return nil +} + +func (p *WorkerId) OpUpdate(ctx *gin.Context, req *worker_idproto.OpUpdateReq) error { + err := p.store.UpdateWorkerId(ctx, req.WorkerId) + if err != nil { + logger.Error("UpdateWorkerId fail, err: %v", err) + return err + } + return nil +} + +func (p *WorkerId) OpDelete(ctx *gin.Context, id int64) error { + err := p.store.DeleteWorkerId(ctx, id) + if err != nil { + logger.Error("DeleteWorkerId fail, err: %v", err) + return err + } + return nil +} + +func (p *WorkerId) OpListByMid(ctx *gin.Context, req *worker_idproto.OpListByMidReq) (*dbstruct.WorkerId, error) { + workerId, err := p.store.GetWorkerIdByMid(ctx, req) + if err != nil { + logger.Error("GetWorkerIdList fail, err: %v", err) + return nil, err + } + return workerId, nil +} diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 93468f13..312a4253 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -2,6 +2,7 @@ package service import ( "encoding/base64" + "encoding/json" "errors" "fmt" "service/api/base" @@ -21,6 +22,7 @@ import ( daily_statement_zone_info_proto "service/api/proto/daily_statement_zone_info/proto" feedbackproto "service/api/proto/feedback/proto" footprintproto "service/api/proto/footprint/proto" + hvyogoproto "service/api/proto/hvyogo/proto" imageaudittaskproto "service/api/proto/imageaudittask/proto" loginproto "service/api/proto/login/proto" mediaproto "service/api/proto/media/proto" @@ -39,6 +41,7 @@ import ( userwxaddcheckproto "service/api/proto/userwxaddcheck/proto" vasproto "service/api/proto/vas/proto" vericodeproto "service/api/proto/vericode/proto" + workeridproto "service/api/proto/worker_id/proto" zoneproto "service/api/proto/zone/proto" zone_collaborator_proto "service/api/proto/zone_collaborator/proto" zone_third_partner_proto "service/api/proto/zone_third_partner/proto" @@ -131,6 +134,7 @@ var ( _DefaultStreamerAcct *logic.StreamerAcct _DefaultContentAuditRTI *logic.ContentAuditRTI _DefaultStreamerScore *logic.StreamerScore + _DefaultWorkerId *logic.WorkerId ) type Service struct { @@ -222,6 +226,7 @@ func (s *Service) Init(c any) (err error) { _DefaultDailyStatementZoneInfo = logic.NewDailyStatementZoneInfo(store) _DefaultContentAuditRTI = logic.NewContentAuditRTI(store) _DefaultStreamerScore = logic.NewStreamerScore(store) + _DefaultWorkerId = logic.NewWorkerId(store) _DefaultVas = logic.NewVas(store, _DefaultStreamer, _DefaultAccount, _DefaultZone, _DefaultZoneThirdPartner, _DefaultZoneCollaborator) _DefaultStreamerAcct = logic.NewStreamerAcct(store) @@ -4058,3 +4063,98 @@ func (s *Service) OpGetDailyStatementZoneInfoList(ctx *gin.Context, req *daily_s return } + +// WorkerId +func (s *Service) OpCreateWorkerId(ctx *gin.Context, req *workeridproto.OpCreateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeWorkerIdSrvOk + err := _DefaultWorkerId.OpCreate(ctx, req) + if err != nil { + logger.Error("OpCreate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + return +} + +func (s *Service) OpUpdateWorkerId(ctx *gin.Context, req *workeridproto.OpUpdateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeWorkerIdSrvOk + err := _DefaultWorkerId.OpUpdate(ctx, req) + if err == qmgo.ErrNoSuchDocuments { + ec = errcode.ErrCodeWorkerIdNotExist + err = nil + return + } + if err != nil { + logger.Error("OpUpdate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + return +} + +func (s *Service) OpDeleteWorkerId(ctx *gin.Context, id int64) (ec errcode.ErrCode) { + ec = errcode.ErrCodeWorkerIdSrvOk + err := _DefaultWorkerId.OpDelete(ctx, id) + if err != nil { + logger.Error("OpDelete fail, id: %v, err: %v", id, err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + return +} + +func (s *Service) OpGetWorkerIdByMid(ctx *gin.Context, req *workeridproto.OpListByMidReq) (workerId *dbstruct.WorkerId, ec errcode.ErrCode) { + ec = errcode.ErrCodeWorkerIdSrvOk + workerId, err := _DefaultWorkerId.OpListByMid(ctx, req) + if err != nil { + logger.Error("OpGetWorkerIdByMid fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + return +} + +func (s *Service) SaveHvyogoCallback(ctx *gin.Context, req *hvyogoproto.HvyogoCallbackReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeWorkerIdSrvOk + + // 解析BusinessBody + vo := &hvyogoproto.HvyogoVO{} + if err := json.Unmarshal([]byte(req.BusinessBody), vo); err != nil { + logger.Error("Unmarshal fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeAssertionFail + return + } + + if vo.AgreeState != consts.HygAgreeState_Success { + return + } + + // 从手机号查找mid + phonehash := mycrypto.CryptoServiceInstance().SHA256.Encrypt([]byte(req.CooperatorId)) + list, err := _DefaultAccount.OpListByPhoneHash(ctx, phonehash) + if err != nil { + logger.Error("OpListByPhoneHash fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeAccountSrvFail + return + } + if len(list) == 0 { + logger.Error("No account entity was found, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeAccountNotExist + return + } + + // 存入数据 + err = _DefaultWorkerId.OpCreate(ctx, &workeridproto.OpCreateReq{ + WorkerId: &dbstruct.WorkerId{ + Mid: list[0].Mid, + WorkerId: goproto.String(vo.WorkerId), + }, + }) + if err != nil { + logger.Error("_DefaultWorkerId OpCreate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + + return +} diff --git a/codecreate/codecreate.go b/codecreate/codecreate.go index 3b7a2bd5..f6020657 100644 --- a/codecreate/codecreate.go +++ b/codecreate/codecreate.go @@ -9,10 +9,10 @@ import ( func main() { genSource := &generator.GenSource{ - EntityName: "DailyStatementZoneInfo", - ModuleName: "daily_statement_zone_info", - EntityCNName: "空间相关每日报表", - ErrCodeSeq: "40", + EntityName: "WorkerId", + ModuleName: "worker_id", + EntityCNName: "用户职业者id映射表", + ErrCodeSeq: "41", } generator.CreateFileDirectory(genSource) diff --git a/codecreate/resource/EntityDefine.xlsx b/codecreate/resource/EntityDefine.xlsx index de6f31c5..e87515d0 100644 Binary files a/codecreate/resource/EntityDefine.xlsx and b/codecreate/resource/EntityDefine.xlsx differ diff --git a/dbstruct/worker_id.go b/dbstruct/worker_id.go new file mode 100644 index 00000000..07796a20 --- /dev/null +++ b/dbstruct/worker_id.go @@ -0,0 +1,11 @@ +package dbstruct + +type WorkerId struct { + Id *int64 `json:"id" bson:"_id"` // 慧用工职业者id表id + Mid *int64 `json:"mid" bson:"mid"` // 用户id + WorkerId *string `json:"worker_id" bson:"worker_id"` // 职业者id + Ct *int64 `json:"ct" bson:"ct"` // 创建时间 + Ut *int64 `json:"ut" bson:"ut"` // 更新时间 + DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记 + +} diff --git a/etc/mix/mix-test.yaml b/etc/mix/mix-test.yaml index 26445c97..75c237a5 100644 --- a/etc/mix/mix-test.yaml +++ b/etc/mix/mix-test.yaml @@ -55,6 +55,11 @@ crypto: private_key: "" sha256: salts: "23XSka ZsMTz3 xdnKtT mx229Z aJ3VS7 ett3nV YDEgFB lBm57J 92hgkJ B0u9VF keyNK3 cB16Tm 63r1mX IUs471 ytbW7o ic8Pxw" + hyg_aes: + private_key: "Xbz1145141919810" + hyg_rsa: + private_key: "MIIBOgIBAAJBAMXPIjKV6CMi5O9tIXJWNIfnqXjqOZ1KmRByRAP073DU+gzMLygzEsrztJzbz/K/Julkz6XhheZ8vdz+boAl1HsCAwEAAQJAD5PNRKCLhGDWGIjGjoN9ZYp3zR7c5KcgQKxO8OTUClWHxXWAHSO8WEMDUjYTB9xhTbzyyjfOV7GJf2YFRgQUYQIhANEFkyp7xKLIq284zmvJ+YAqMMqc5Wxkz/0Kv77PERfxAiEA8kRrSX2QcObiQkE2Jxn4VlW8M+bHZMdfabjSkYgKvysCIHGYhweCpjYVut3CUKTR6q/VvhiPNjw3ebW6vsSTQmXxAiBBe4DMQmYyPhXV9q5eb0SSgWX3WV93u/PsLYqsz2qoNQIhAKiqbRvYMzfg+OTBKuuGu3m+QLav4TjYVSSPD8VVf7/z" + alipay: appid: "2021004115647165" diff --git a/library/configcenter/configcenter.go b/library/configcenter/configcenter.go index 2ce526a5..8b6f56df 100644 --- a/library/configcenter/configcenter.go +++ b/library/configcenter/configcenter.go @@ -74,10 +74,12 @@ type WxpayClientConfig struct { // 账号相关验密配置 type CryptoConfig struct { - *AESConfig `json:"aes" yaml:"aes"` //AES - *RSAConfig `json:"rsa" yaml:"rsa"` //RSA - *TokenConfig `json:"token" yaml:"token"` //token签名 - *SHA256Config `json:"sha256" yaml:"sha256"` //SHA256 + *AESConfig `json:"aes" yaml:"aes"` // AES + *RSAConfig `json:"rsa" yaml:"rsa"` // RSA + *TokenConfig `json:"token" yaml:"token"` // token签名 + *SHA256Config `json:"sha256" yaml:"sha256"` // SHA256 + *HygAESConfig `json:"hyg_aes" yml:"hyg_aes"` // 慧用工的AES + *HygRSAConfig `json:"hyg_rsa" yaml:"hyg_rsa"` // 慧用工的RSA } // 支付宝客户端配置 diff --git a/library/configcenter/cryptoconfigcenter.go b/library/configcenter/cryptoconfigcenter.go index b17793da..dc9c248c 100644 --- a/library/configcenter/cryptoconfigcenter.go +++ b/library/configcenter/cryptoconfigcenter.go @@ -15,3 +15,11 @@ type TokenConfig struct { type SHA256Config struct { SHA256Salts string `json:"salts" yaml:"salts"` //SHA256 salts } + +type HygAESConfig struct { + HygAESPrivateKey string `json:"private_key" yaml:"private_key"` //AES私钥 +} + +type HygRSAConfig struct { + HygRSAPrivateKey string `json:"private_key" yaml:"private_key"` //AES私钥 +} diff --git a/library/idgenerator/genid.go b/library/idgenerator/genid.go index 0043b581..806f8171 100644 --- a/library/idgenerator/genid.go +++ b/library/idgenerator/genid.go @@ -53,6 +53,7 @@ const ( NodeZoneSession // node 空间对话表 NodeZoneThirdPartner // node 空间代运营表 NodeDailyStatementZoneInfo // node 空间相关每日报表 + NodeWorkerId // node 用户职业者id映射表 ) func GenIdInt64(node int64) (int64, error) { @@ -252,3 +253,9 @@ func GenDailyStatementZoneInfoId() int64 { id, _ := GenIdInt64(NodeDailyStatementZoneInfo) return id } + +// worker_id +func GenWorkerIdId() int64 { + id, _ := GenIdInt64(NodeWorkerId) + return id +} diff --git a/library/mycrypto/aesCrypto.go b/library/mycrypto/aesCrypto.go index 5e6c52ba..9ed17fdc 100644 --- a/library/mycrypto/aesCrypto.go +++ b/library/mycrypto/aesCrypto.go @@ -23,6 +23,14 @@ func NewAesCrypto(cryptoConfig *configcenter.CryptoConfig) (aesCrypto *AesCrypto return } +func NewAesCryptoFromString(aesPrivateKeyStr string) (aesCrypto *AesCrypto, err error) { + aesCrypto = &AesCrypto{} + + //读取私钥 + aesCrypto.aesPriKey = []byte(aesPrivateKeyStr) + return +} + func (aesCrypto *AesCrypto) Encrypt(msg []byte) (encryptedBytes []byte, err error) { //CBC加密 block, err := aes.NewCipher(aesCrypto.aesPriKey) //block diff --git a/library/mycrypto/cryptoService.go b/library/mycrypto/cryptoService.go index 2ae5174c..718255c1 100644 --- a/library/mycrypto/cryptoService.go +++ b/library/mycrypto/cryptoService.go @@ -22,6 +22,8 @@ type CryptoService struct { RSA *RsaCrypto AES *AesCrypto SHA256 *Sha256Crypto + HygRSA *RsaCrypto + HygAES *AesCrypto } func (cryptoService *CryptoService) Init(cryptoConfig *configcenter.CryptoConfig) (err error) { @@ -39,5 +41,16 @@ func (cryptoService *CryptoService) Init(cryptoConfig *configcenter.CryptoConfig logger.Error("Init SHA256 Service failed! err:%v", err) return } + + if cryptoService.HygRSA, err = NewRsaCryptoFromString(cryptoConfig.HygRSAPrivateKey); err != nil { + logger.Error("Init HYG RSA Service failed! err:%v", err) + return + } + + if cryptoService.HygAES, err = NewAesCryptoFromString(cryptoConfig.HygAESPrivateKey); err != nil { + logger.Error("Init HYG AES Service failed! err:%v", err) + return + } + return } diff --git a/library/mycrypto/rsaCrypto.go b/library/mycrypto/rsaCrypto.go index a023f385..5031bc34 100644 --- a/library/mycrypto/rsaCrypto.go +++ b/library/mycrypto/rsaCrypto.go @@ -28,6 +28,24 @@ func NewRsaCrypto(cryptoConfig *configcenter.CryptoConfig) (rsaCrypto *RsaCrypto return } +func NewRsaCryptoFromString(rsaPrivateKeyStr string) (rsaCrypto *RsaCrypto, err error) { + rsaCrypto = &RsaCrypto{} + + //读取私钥 + rsaPriKey, err := readRSAPriKeyFromString(rsaPrivateKeyStr) + if err != nil { + logger.Error("read rsa primary key failed!", err) + return + } + + //读取公钥 + rsaPubKey := &rsaPriKey.PublicKey + + rsaCrypto.rsaPriKey = rsaPriKey + rsaCrypto.rsaPubKey = rsaPubKey + return +} + func (rsaCrypto *RsaCrypto) Encrypt(msg []byte) (encryptedBytes []byte, err error) { //公钥加密 encryptedBytes, err = rsa.EncryptPKCS1v15(rand.Reader, rsaCrypto.rsaPubKey, msg) @@ -68,3 +86,14 @@ func (rsaCrypto *RsaCrypto) readRSAPriKey(cryptoConfig *configcenter.CryptoConfi } return } + +func readRSAPriKeyFromString(rsaPrivateKeyStr string) (rsaPriKey *rsa.PrivateKey, err error) { + //读取私钥 + rsaPriKeyBytes, _ := base64.StdEncoding.DecodeString(rsaPrivateKeyStr) + rsaPriKey, err = x509.ParsePKCS1PrivateKey(rsaPriKeyBytes) + if err != nil { + logger.Error("Decoding rsa primary key failed, check your config, please!, err: %v", err) + return + } + return +} diff --git a/library/taginterceptor/decryptTagInterceptor.go b/library/taginterceptor/decryptTagInterceptor.go index 38c33d29..9c1b6020 100644 --- a/library/taginterceptor/decryptTagInterceptor.go +++ b/library/taginterceptor/decryptTagInterceptor.go @@ -18,6 +18,8 @@ func newDecryptTagInterceptor() *DecryptTagInterceptor { cryptoFuncMap := make(map[string]cryptoFunc) cryptoFuncMap["aes_cbc"] = mycrypto.CryptoServiceInstance().AES.Decrypt cryptoFuncMap["rsa"] = mycrypto.CryptoServiceInstance().RSA.Decrypt + cryptoFuncMap["hyg_rsa"] = mycrypto.CryptoServiceInstance().HygRSA.Decrypt + cryptoFuncMap["hyg_aes"] = mycrypto.CryptoServiceInstance().HygAES.Decrypt cryptoTagInterceptor := newCryptoTagInterceptor() cryptoTagInterceptor.LoadPrintableCryptoFunc(decryptTagInterceptor.getPrintableCryptoFunc(cryptoFuncMap)) diff --git a/library/taginterceptor/encryptTagInterceptor.go b/library/taginterceptor/encryptTagInterceptor.go index 1e12d224..0a191045 100644 --- a/library/taginterceptor/encryptTagInterceptor.go +++ b/library/taginterceptor/encryptTagInterceptor.go @@ -18,6 +18,8 @@ func newEncryptTagInterceptor() *EncryptTagInterceptor { cryptoFuncMap := make(map[string]cryptoFunc) cryptoFuncMap["aes_cbc"] = mycrypto.CryptoServiceInstance().AES.Encrypt cryptoFuncMap["rsa"] = mycrypto.CryptoServiceInstance().RSA.Encrypt + cryptoFuncMap["hyg_rsa"] = mycrypto.CryptoServiceInstance().HygRSA.Encrypt + cryptoFuncMap["hyg_aes"] = mycrypto.CryptoServiceInstance().HygAES.Encrypt cryptoTagInterceptor := newCryptoTagInterceptor() cryptoTagInterceptor.LoadPrintableCryptoFunc(encryptTagInterceptor.getPrintableCryptoFunc(cryptoFuncMap))