Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Last active May 18, 2021 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/b0f1c3418d0b40d37d945905f90c768d to your computer and use it in GitHub Desktop.
Save kyrathasoft/b0f1c3418d0b40d37d945905f90c768d to your computer and use it in GitHub Desktop.
Library for returning SHA1 and MD5 hashes
/* This code was copied by Bryan Miller 16th May 2021, but the actual prime detection
methods contain code found on various websites and forums. */
using System;
using System.Security.Cryptography;
using System.Text;
namespace GetHashNs {
public class GetHashCls {
/* SHA1 is no longer considered a highly secure hashing algorithm. However,
we'll often she SHA1 hashes used to check integrity of downloads.
It should be noted that the hash functions used to protect passwords are not
the same as the hash functions you may have seen in a data structures course.
The hash functions used to implement data structures such as hash tables are
designed to be fast, not secure. Only cryptographic hash functions may be used
to implement password hashing. Hash functions like SHA256, SHA512, RipeMD, and
WHIRLPOOL are cryptographic hash functions. */
public static string GetSHA1 (string ipString) {
SHA1 sha1 = new SHA1CryptoServiceProvider ();
byte[] ipBytes = Encoding.Default.GetBytes (ipString.ToCharArray ());
byte[] opBytes = sha1.ComputeHash (ipBytes);
StringBuilder stringBuilder = new StringBuilder (40);
for (int i = 0; i < opBytes.Length; i++) {
stringBuilder.Append (opBytes[i].ToString ("x2"));
}
return stringBuilder.ToString ();
}
/* MD5 isn’t recommended anymore because it is designed to be very fast
and efficient. By using modern computers and techniques, it is possible to
“brute force” the hash output, in order to retrieve the original input.
Because of this, many security experts suggest not to use it for password
hashing. */
public static string GetMD5 (string ipString) {
MD5 md5 = new MD5CryptoServiceProvider ();
byte[] ipBytes = Encoding.Default.GetBytes (ipString.ToCharArray ());
byte[] opBytes = md5.ComputeHash (ipBytes);
StringBuilder stringBuilder = new StringBuilder (40);
for (int i = 0; i < opBytes.Length; i++) {
stringBuilder.Append (opBytes[i].ToString ("x2"));
}
return stringBuilder.ToString ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment