service/library/mycrypto/cryptoService.go

57 lines
1.2 KiB
Go

package mycrypto
import (
"service/library/configcenter"
"service/library/logger"
"sync"
)
var (
instance *CryptoService
once sync.Once
)
func CryptoServiceInstance() *CryptoService {
once.Do(func() {
instance = &CryptoService{}
})
return instance
}
type CryptoService struct {
RSA *RsaCrypto
AES *AesCrypto
SHA256 *Sha256Crypto
HygRSA *RsaCrypto
HygAES *AesCrypto
}
func (cryptoService *CryptoService) Init(cryptoConfig *configcenter.CryptoConfig) (err error) {
if cryptoService.RSA, err = NewRsaCrypto(cryptoConfig); err != nil {
logger.Error("Init RSA Service failed! err:%v", err)
return
}
if cryptoService.AES, err = NewAesCrypto(cryptoConfig); err != nil {
logger.Error("Init AES Service failed! err:%v", err)
return
}
if cryptoService.SHA256, err = NewSha256Crypto(cryptoConfig); err != nil {
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
}