-
-
Save 0xbrock/fbdf22aaa8633381e43f3193d6226776 to your computer and use it in GitHub Desktop.
The missing front door code for https://github.com/sdrapkin/SecurityDriven.Inferno.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Encrypted { | |
public string Value { get; set; } | |
public string Salt { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IEncryptionConfiguration { | |
string MasterKey { get; } | |
int SaltSizeInBytes { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IEncryptionProvider { | |
Encrypted Encrypt(string content); | |
string Decrypt(Encrypted content); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment