Skip to content

Instantly share code, notes, and snippets.

@jonfazzaro
Last active April 6, 2018 12:21
Show Gist options
  • Save jonfazzaro/c4a1c113c2a619e98d53662d3440661e to your computer and use it in GitHub Desktop.
Save jonfazzaro/c4a1c113c2a619e98d53662d3440661e to your computer and use it in GitHub Desktop.
The missing front door code for https://github.com/sdrapkin/SecurityDriven.Inferno.
public class Encrypted {
public string Value { get; set; }
public string Salt { get; set; }
}
public interface IEncryptionConfiguration {
string MasterKey { get; }
int SaltSizeInBytes { get; }
}
public interface IEncryptionProvider {
Encrypted Encrypt(string content);
string Decrypt(Encrypted content);
}
using SecurityDriven.Inferno;
using System;
public class InfernoEncryptionProvider : IEncryptionProvider {
readonly IEncryptionConfiguration _configuration;
public InfernoEncryptionProvider(IEncryptionConfiguration configuration) {
_configuration = configuration;
}
public string Decrypt(Encrypted encrypted) {
if (encrypted == null)
return null;
byte[] clearBytes = Decrypt(
Convert.FromBase64String(encrypted.Value).AsArraySegment(),
Convert.FromBase64String(encrypted.Salt).AsArraySegment());
return Utils.SafeUTF8.GetString(clearBytes);
}
public Encrypted Encrypt(string clearText) {
if (string.IsNullOrWhiteSpace(clearText))
return null;
var saltBytes = GenerateSalt();
var cipherBytes = Encrypt(clearText, saltBytes);
return new Encrypted {
Value = Convert.ToBase64String(cipherBytes),
Salt = Convert.ToBase64String(saltBytes)
};
}
private byte[] GenerateSalt() {
return new CryptoRandom().NextBytes(_configuration.SaltSizeInBytes);
}
private byte[] Decrypt(ArraySegment<byte> cipher, ArraySegment<byte> salt) {
return EtM_CBC.Decrypt(_configuration.MasterKey.ToBytes(),
cipher, salt);
}
private byte[] Encrypt(string clearText, byte[] salt) {
return EtM_CBC.Encrypt(_configuration.MasterKey.ToBytes(),
Encoding.UTF8.GetBytes(clearText).AsArraySegment(), salt.AsArraySegment());
}
}
@sdrapkin
Copy link

sdrapkin commented Nov 5, 2016

😃 You had one job - use Inferno's high-level APIs as documented (via SuiteB class):

public static byte[] Encrypt(byte[] masterKey, ArraySegment<byte> plaintext, ArraySegment<byte>? salt = null)
public static byte[] Decrypt(byte[] masterKey, ArraySegment<byte> ciphertext, ArraySegment<byte>? salt = null)

Salt is completely wasted and unnecessary in your case - you should leave it null. Once you remove salt, Encrypted container is also pointless, and so is IEncryptionConfiguration.

If you need to use CryptoRandom somewhere, there is no need to create a new CryptoRandom instance every time – create a static CryptoRandom instance and use it everywhere (it's thread-safe).

Don't use EtM_CBC unless you are sure you need EtM_CBC. Just use the high-level APIs from SuiteB.

@MSACATS
Copy link

MSACATS commented Dec 16, 2016

Where is any of that documented? There are three classes with "Encrypt" and "Decrypt" methods and not one of them has a single comment about usage or parameters, nor is there any example code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment