service/library/mycrypto/cryptoService.go

57 lines
1.3 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
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 {
2024-06-18 17:10:42 +08:00
RSA *RsaCrypto
AES *AesCrypto
SHA256 *Sha256Crypto
HygSHA1WithRSA *SHAwithRSACrypto
HygAES *AesEcbCrypto
2023-12-21 22:17:40 +08:00
}
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
}
2024-06-15 13:41:03 +08:00
2024-06-18 17:10:42 +08:00
if cryptoService.HygSHA1WithRSA, err = NewSHAwithRSACryptoFromString(cryptoConfig.HygRSAPrivateKey); err != nil {
logger.Error("Init HYG SHAwithRSA Service failed! err:%v", err)
2024-06-15 13:41:03 +08:00
return
}
2024-06-18 17:10:42 +08:00
if cryptoService.HygAES, err = NewAesEcbCryptoFromString(cryptoConfig.HygAESPrivateKey); err != nil {
2024-06-15 13:41:03 +08:00
logger.Error("Init HYG AES Service failed! err:%v", err)
return
}
2023-12-21 22:17:40 +08:00
return
}