From 73c22f5a643a20ae504c91b862b6be22917932e4 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 20 Jun 2024 21:31:51 +0700 Subject: Refactoring in ParameterUtilities --- crypto/src/asn1/misc/CAST5CBCParameters.cs | 7 +++---- crypto/src/asn1/misc/IDEACBCPar.cs | 8 ++++---- crypto/src/security/ParameterUtilities.cs | 12 ++++++------ 3 files changed, 13 insertions(+), 14 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/misc/CAST5CBCParameters.cs b/crypto/src/asn1/misc/CAST5CBCParameters.cs index 7bd9f1ec6..3adafe05f 100644 --- a/crypto/src/asn1/misc/CAST5CBCParameters.cs +++ b/crypto/src/asn1/misc/CAST5CBCParameters.cs @@ -45,10 +45,9 @@ namespace Org.BouncyCastle.Asn1.Misc keyLength = (DerInteger) seq[1]; } - public byte[] GetIV() - { - return Arrays.Clone(iv.GetOctets()); - } + public Asn1OctetString IV => iv; + + public byte[] GetIV() => Arrays.Clone(iv.GetOctets()); public int KeyLength { diff --git a/crypto/src/asn1/misc/IDEACBCPar.cs b/crypto/src/asn1/misc/IDEACBCPar.cs index 96bfb89b0..9b73f5729 100644 --- a/crypto/src/asn1/misc/IDEACBCPar.cs +++ b/crypto/src/asn1/misc/IDEACBCPar.cs @@ -1,6 +1,7 @@ using System; using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Misc { @@ -40,10 +41,9 @@ namespace Org.BouncyCastle.Asn1.Misc } } - public byte[] GetIV() - { - return iv == null ? null : iv.GetOctets(); - } + public Asn1OctetString IV => iv; + + public byte[] GetIV() => Arrays.Clone(iv.GetOctets()); /** * Produce an object suitable for an Asn1OutputStream. diff --git a/crypto/src/security/ParameterUtilities.cs b/crypto/src/security/ParameterUtilities.cs index 36f1dbbb2..7a89e8eb8 100644 --- a/crypto/src/security/ParameterUtilities.cs +++ b/crypto/src/security/ParameterUtilities.cs @@ -290,7 +290,7 @@ namespace Org.BouncyCastle.Security if (canonical == null) throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised."); - byte[] iv = null; + Asn1OctetString iv = null; try { @@ -302,19 +302,19 @@ namespace Org.BouncyCastle.Security if (basicIVKeySize != -1 || canonical == "RIJNDAEL" || canonical == "SKIPJACK" || canonical == "TWOFISH") { - iv = ((Asn1OctetString) asn1Params).GetOctets(); + iv = Asn1OctetString.GetInstance(asn1Params); } else if (canonical == "CAST5") { - iv = Cast5CbcParameters.GetInstance(asn1Params).GetIV(); + iv = Cast5CbcParameters.GetInstance(asn1Params).IV; } else if (canonical == "IDEA") { - iv = IdeaCbcPar.GetInstance(asn1Params).GetIV(); + iv = IdeaCbcPar.GetInstance(asn1Params).IV; } else if (canonical == "RC2") { - iv = RC2CbcParameter.GetInstance(asn1Params).GetIV(); + iv = RC2CbcParameter.GetInstance(asn1Params).IV; } } catch (Exception e) @@ -324,7 +324,7 @@ namespace Org.BouncyCastle.Security if (iv != null) { - return new ParametersWithIV(key, iv); + return new ParametersWithIV(key, iv.GetOctets()); } throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised."); -- cgit 1.4.1