Skip to content

Commit

Permalink
Fixing issue related to salt-to-byte conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
smithc committed Aug 5, 2016
1 parent f139add commit b0ee1a6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Jabberwocky.Core/Cryptography/AesHmacCryptoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static AesHmacCryptoService Create(string secretKey, string digestKey, st
public AesHmacCryptoService(CryptoConfiguration config, ISerializationProvider serializationProvider)
{
if (serializationProvider == null) throw new ArgumentNullException(nameof(serializationProvider));
if (!IsCryptoConfigurationValid(config)) throw new ArgumentException("All configuration properties must be valid.", nameof(config));
SerializationProvider = serializationProvider;

_lazyDerivedBytes = new Lazy<KeySaltPair>(() => GenerateDerivedBytes(config));
Expand Down Expand Up @@ -147,7 +148,9 @@ protected virtual byte[] ComputeHash(byte[] content)
private static KeySaltPair GenerateDerivedBytes(CryptoConfiguration config)
{
var saltBytes = new byte[SaltSize];
Encoding.UTF8.GetBytes(config.InitializationVector, 0, config.InitializationVector.Length, saltBytes, 0);
var charSizeInBytes = sizeof(char);
var characterCount = Math.Min(SaltSize / charSizeInBytes, config.InitializationVector.Length);
Encoding.UTF8.GetBytes(config.InitializationVector, 0, characterCount, saltBytes, 0);

using (var derivePassword = new Rfc2898DeriveBytes(config.SecretKey, saltBytes))
{
Expand All @@ -163,6 +166,13 @@ private static KeySaltPair GenerateDerivedBytes(CryptoConfiguration config)
}
}

private static bool IsCryptoConfigurationValid(CryptoConfiguration config)
{
return !string.IsNullOrEmpty(config.DigestKey)
&& !string.IsNullOrEmpty(config.InitializationVector)
&& !string.IsNullOrEmpty(config.SecretKey);
}

#endregion

protected struct KeySaltPair
Expand Down

0 comments on commit b0ee1a6

Please sign in to comment.