Skip to main content

.Net 2.0: Hash with Salt using SecureString

Cryptography Simplified in Microsoft .NET
Security Guidelines: .NET Framework 2.0

Ideally, we would return a SecureString here and make the consuming developer work with that but for our example...

public string HashInput(string input, int saltLength)
{
byte[] ssBytes;

// create salt
byte[] bytSalt = new byte[saltLength];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytSalt);

// create secure string for concatinating input and salt
using (SecureString ss = new SecureString())
{
// append original string
foreach (char c in input.ToCharArray())
{
ss.AppendChar(c);
}

// append salt
foreach (byte b in bytSalt)
{
ss.AppendChar(Convert.ToChar(b));
}

// prevent SecureString manipulation
ss.MakeReadOnly();

// instantiate hash provider
SHA512Managed sha = new SHA512Managed();

// pointer to hold unmanaged reference to SecureString instance
IntPtr bstr = IntPtr.Zero;

try
{
// marshall SecureString into byte array
ssBytes = new byte[ss.Length * 2];
Marshal.Copy((bstr = Marshal.SecureStringToBSTR(ss)),
ssBytes, 0, ssBytes.Length);
}
finally
{
// Make sure that the clear text data is zeroed out
Marshal.ZeroFreeBSTR(bstr);
}

// hash byte array
byte[] hashed = sha.ComputeHash(ssBytes);

// clear the provider memory
sha.Clear();

return Convert.ToBase64String(hashed);
}
}

Comments

Popular posts from this blog

Hero Write-up: Now this is Customer Service!

My best friend Scott is president/C-everything of a small northeastern Ohio manufacturing concern, KirkKey Interlock . I hadn't spoken with him for a while and wanted to see how Canton fared with the Blizzard of '08 (that's what they're calling it...not me). I say, "So what's new?" He replies that on Tuesday his primary server (which essentially runs the business) came up with lame with not one, but [a statistically improbable] *two* physical disk failures on a RAID5 hardware array. My friend attempts the fix but gives up pretty quickly after seeing some Linux nasty-grams on the boot screen. His service provider is an old college buddy who lives down in Raleigh, Cerient Technologies led by Jason Tower . Scott couldn't email out because Exchange was on the toasted server. Being creative, Scott started Treo-emailing photos of the screen. Unfortunately, Jason couldn't receive email because a storm had knocked out a lot of local hosting. [Sigh] After...

Certified or Certifiable?

As a senior technology professional, I interview a lot of candidates. I also maintain solid relationships with other folks in the community. Frequently, the topic of certifications arises: A good investment? Valuable? A clear measurement of skill? Consensus appears to draw the line related to one's seniority. If you're (for example) just out of school and looking for an instant creditability boost, by all means pursue a certification. Likely, this credential will assist you in overcoming the "junior" tag and likely land you more interviews and client roles. (Note: I'm going to use the terms senior and junior here...no offense to either. Can't think of a better one word description. I was a junior once too.) In stark contrast, the value of certifications drops off the table around the 2-3 year mark. Some in my circles even perceive certifications as a negative for the senior professional. They think, "If this guy is so solid, why is he wasting valuable...

Consulting Exodus Trend?

Is it just me or have a significant number of 'A' players left our consulting firms? People come and people go. Ours is certainly not an industry of "lifers". However, within the past year or so, I've witnessed several of my consulting peers -- the folks I really look up to -- leave the consulting arena for [predominately] full-time technology product firms. A smaller number have left for full-time positions at businesses while an even smaller number left to start their own business|firm|freelance|etc. Their departure struck me as odd because these were the type of folks who [I thought] would eventually become owner / partners at their respective firms. Certainly, the firms will carry on and continue to perform well but the departure of these folks would result in nothing less than a severe case of the hiccups and quite possibly a minor cardiac event. You know who you are. Please comment. Do we [the consulting industry] have a brain drain issue? Is this a norm...