Skip to content

Commit

Permalink
Remove a few initialization allocations from Cng (dotnet/corefx#41373)
Browse files Browse the repository at this point in the history
Save four "startup" allocations in System.Security.Cryptography.Cng.

- Appending the '\0' is better done at the call site, because then the concat is done with two constants at compile time.
- There's no benefit to using ToCharArray instead of just passing in the string.
- Once we get rid of those, we can just inline the construction into the declarations.

Commit migrated from dotnet/corefx@296c0e7
  • Loading branch information
stephentoub authored Sep 27, 2019
1 parent 014e734 commit f322904
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,12 @@ private void Reset()
}
}

private static CngProperty CreateCngPropertyForCipherMode(string cipherMode)
{
byte[] cipherModeBytes = Encoding.Unicode.GetBytes((cipherMode + "\0").ToCharArray());
return new CngProperty(Interop.NCrypt.NCRYPT_CHAINING_MODE_PROPERTY, cipherModeBytes, CngPropertyOptions.None);
}

private CngKey _cngKey;
private readonly bool _encrypting;

private static readonly CngProperty s_ECBMode = CreateCngPropertyForCipherMode(Interop.BCrypt.BCRYPT_CHAIN_MODE_ECB);
private static readonly CngProperty s_CBCMode = CreateCngPropertyForCipherMode(Interop.BCrypt.BCRYPT_CHAIN_MODE_CBC);
private static readonly CngProperty s_ECBMode =
new CngProperty(Interop.NCrypt.NCRYPT_CHAINING_MODE_PROPERTY, Encoding.UTF8.GetBytes(Interop.BCrypt.BCRYPT_CHAIN_MODE_ECB + "\0"), CngPropertyOptions.None);
private static readonly CngProperty s_CBCMode =
new CngProperty(Interop.NCrypt.NCRYPT_CHAINING_MODE_PROPERTY, Encoding.UTF8.GetBytes(Interop.BCrypt.BCRYPT_CHAIN_MODE_CBC + "\0"), CngPropertyOptions.None);
}
}

0 comments on commit f322904

Please sign in to comment.