Created
August 25, 2011 15:32
-
-
Save jasondentler/1170933 to your computer and use it in GitHub Desktop.
IUserType encrypted string
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" | |
assembly="ACC.Testing.Core" | |
namespace="ACC.Testing"> | |
<typedef class="ACC.Testing.Data.EncryptedString, ACC.Testing.Data" name="encrypted" /> | |
<class name="Account"> | |
<id name="Id"> | |
<generator class="guid.comb" /> | |
</id> | |
<natural-id> | |
<property name="EMail" not-null="true" /> | |
</natural-id> | |
<property name="Name" not-null="true" /> | |
<property name="Password" not-null="true" /> | |
<property name="SSN" not-null="true" | |
unique="true" index="Account_SSN" | |
type="encrypted"/> | |
<property name="PersonId" not-null="true" | |
unique="true" | |
index="Account_PersonId" /> | |
</class> | |
</hibernate-mapping> |
This file contains hidden or 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 System; | |
using System.Data; | |
using System.Runtime.Serialization; | |
using ACC.Testing.Security; | |
using Microsoft.Practices.ServiceLocation; | |
using NHibernate; | |
using NHibernate.SqlTypes; | |
using NHibernate.UserTypes; | |
namespace ACC.Testing.Data | |
{ | |
[Serializable] | |
public class EncryptedString : IUserType, ISerializable | |
{ | |
private readonly IEncryptor _encryptor; | |
public EncryptedString() | |
{ | |
IServiceLocator sl = null; | |
try | |
{ | |
sl = ServiceLocator.Current; | |
} catch (Exception exception) | |
{ | |
throw new ApplicationException("Error getting current service locator", exception); | |
} | |
if (sl == null) throw new NullReferenceException("Common Service Locator not set."); | |
try | |
{ | |
_encryptor = sl.GetInstance<IEncryptor>(); | |
} | |
catch (Exception exception) | |
{ | |
throw new ApplicationException("Error getting IEncryptor for EncryptedString user type.", exception); | |
} | |
} | |
public object NullSafeGet( | |
IDataReader rs, | |
string[] names, | |
object owner) | |
{ | |
//treat for the posibility of null values | |
object passwordString = | |
NHibernateUtil.String.NullSafeGet(rs, names[0]); | |
return passwordString != null | |
? _encryptor.Decrypt((string) passwordString) | |
: null; | |
} | |
public void NullSafeSet( | |
IDbCommand cmd, | |
object value, | |
int index) | |
{ | |
if (value == null) | |
{ | |
NHibernateUtil.String.NullSafeSet(cmd, null, index); | |
return; | |
} | |
string encryptedValue = _encryptor.Encrypt((string) value); | |
NHibernateUtil.String.NullSafeSet( | |
cmd, encryptedValue, index); | |
} | |
public object DeepCopy(object value) | |
{ | |
return value == null | |
? null | |
: string.Copy((string) value); | |
} | |
public object Replace(object original, | |
object target, object owner) | |
{ | |
return original; | |
} | |
public object Assemble(object cached, object owner) | |
{ | |
return DeepCopy(cached); | |
} | |
public object Disassemble(object value) | |
{ | |
return DeepCopy(value); | |
} | |
public SqlType[] SqlTypes | |
{ | |
get { return new[] {new SqlType(DbType.String)}; } | |
} | |
public Type ReturnedType | |
{ | |
get { return typeof (string); } | |
} | |
public bool IsMutable | |
{ | |
get { return false; } | |
} | |
public new bool Equals(object x, object y) | |
{ | |
if (ReferenceEquals(x, y)) | |
return true; | |
if (x == null || y == null) | |
return false; | |
return x.Equals(y); | |
} | |
public int GetHashCode(object x) | |
{ | |
if (x == null) | |
throw new ArgumentNullException("x"); | |
return x.GetHashCode(); | |
} | |
/// <summary> | |
/// Serialize EncryptedStringSerializationHelper instead | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
public void GetObjectData(SerializationInfo info, StreamingContext context) | |
{ | |
info.SetType(typeof(EncryptedStringSerializationHelper)); | |
} | |
[Serializable] | |
internal sealed class EncryptedStringSerializationHelper : IObjectReference | |
{ | |
/// <summary> | |
/// Returns a new EncryptedString instance | |
/// </summary> | |
/// <param name="context"></param> | |
/// <returns></returns> | |
/// <remarks>Avoid serializing the encryption stuff</remarks> | |
public object GetRealObject(StreamingContext context) | |
{ | |
return new EncryptedString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment