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 HygSHA1WithRSA *SHAwithRSACrypto HygAES *AesEcbCrypto MD5 *Md5Crypto } 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.HygSHA1WithRSA, err = NewSHAwithRSACryptoFromString(cryptoConfig.HygRSAPrivateKey); err != nil { logger.Error("Init HYG SHAwithRSA Service failed! err:%v", err) return } if cryptoService.HygAES, err = NewAesEcbCryptoFromString(cryptoConfig.HygAESPrivateKey); err != nil { logger.Error("Init HYG AES Service failed! err:%v", err) return } cryptoService.MD5 = NewMd5Crypto() return }