Skip to content

Instantly share code, notes, and snippets.

@kleewho
Created September 15, 2023 09:06
Show Gist options
  • Save kleewho/ddee71eee8c1c09835a13ed126db8bce to your computer and use it in GitHub Desktop.
Save kleewho/ddee71eee8c1c09835a13ed126db8bce to your computer and use it in GitHub Desktop.
func NewLegacyCryptoModule(cipherKey string, randomIv bool) (CryptorModule, error) {
legacyCryptoAlgorithm, e := NewLegacyCryptoAlgorithm(cipherKey, randomIv)
if e != nil {
return nil, e
}
aesCBCCryptoAlgorithm, e := NewAesCBCCryptoAlgorithm(cipherKey)
if e != nil {
return nil, e
}
decryptors := []CryptoAlgorithm{aesCBCCryptoAlgorithm}
return NewCryptoModule(legacyCryptoAlgorithm, decryptors), nil
}
func NewDefaultCryptoModule(cipherKey string, randomIv bool) (CryptorModule, error) {
aesCBCCryptoAlgorithm, e := NewAesCBCCryptoAlgorithm(cipherKey)
if e != nil {
return nil, e
}
legacyCryptoAlgorithm, e := NewLegacyCryptoAlgorithm(cipherKey, randomIv)
if e != nil {
return nil, e
}
decryptors := []CryptoAlgorithm{legacyCryptoAlgorithm}
return NewCryptoModule(aesCBCCryptoAlgorithm, decryptors), nil
}
func NewCryptoModule(defaultCryptoAlgorithm CryptoAlgorithm, decryptingAlgorithms []CryptoAlgorithm) CryptorModule {
decryptors := make(map[string]cryptorWrapper, len(decryptingAlgorithms)+1)
for _, decryptor := range decryptingAlgorithms {
decryptors[decryptor.Id()] = NewCryptor(decryptor)
}
encryptor := NewCryptor(defaultCryptoAlgorithm)
decryptors[defaultCryptoAlgorithm.Id()] = encryptor
return &Module{
encryptor: encryptor,
decryptors: decryptors,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment