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

Fast and Reliable Home Internet: Your Livelihood Depends on It

You're on yet another Zoom call and...wait what did she say? Dang it...Internet glitching again! You quickly mute your audio and video. "Kids! Get off YouTube...I'm on a call!" With everyone working and schooling from home, your Internet can't keep up. The cable company keeps claiming you're on their "super-fast Internet" but everything keeps lagging. It's all so frustrating and you just want to get your work done.  It may not be the cable company's fault. Use this approach to ensure your household enjoys a super-fast, reliable Internet! Start with the Source Run a speed test. Google "speed test" . Run that test a few times on a given day. If you're not getting at least 50Mbps download and 10Mbps upload speeds, keep reading.  Check with your Provider and do your Homework Reach out to your Internet provider. This may be your cable company or telephone provider. Understand your current plan: What package are you currently on? Wha...

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...

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...