Skip to content

Instantly share code, notes, and snippets.

@akosijose
Last active November 6, 2022 14:08
Show Gist options
  • Save akosijose/97a69a412b358ba839f455c510490b36 to your computer and use it in GitHub Desktop.
Save akosijose/97a69a412b358ba839f455c510490b36 to your computer and use it in GitHub Desktop.
/// <summary>
/// Calculates the SHA-256 hash of a string.
/// </summary>
/// <param name="inputString">String to be hashed.</param>
/// <returns>A SHA-256 string.</returns>
public static string GetSHA256String(string inputString) {
StringBuilder result = new StringBuilder();
using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create()) {
byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(inputString));
for (int i = 0; i < hash.Length; i++) {
result.Append(hash[i].ToString("x2"));
}
}
return result.ToString();
}
public static string TextTrim(string data) {
//Purpose: We need to be able trim a string when it is not null.
// If it is null, you can't trim it because it will trigger a run-time error.
string result = data;
// Check if the string has a value
if (string.IsNullOrEmpty(data) == false) {
// If so, trim the string
result = data.Trim();
}
return result;
}
public static string SQL_NULL_Handler(string text) {
string result = string.Empty;
if (string.IsNullOrEmpty(text) == true) {
result = "NULL";
} else {
result = "'" + text + "'";
}
return result;
}
public static object SQL_DBNULL_Handler(string data) {
object result = DBNull.Value;
if (string.IsNullOrEmpty(data) == false) {
result = data;
}
return result;
}
public static object SQL_NULL_Handler(short? data) {
object result = DBNull.Value;
if (data.HasValue == true) {
result = data.Value;
}
return result;
}
public static object SQL_NULL_Handler(bool? data) {
object result = DBNull.Value;
if (data.HasValue == true) {
result = data.Value;
}
return result;
}
public static object SQL_NULL_Handler(DateTime? data) {
object result = DBNull.Value;
if (data.HasValue == true) {
result = data.Value;
}
return result;
}
public static string SQL_STRING(string text) {
string result = string.Empty;
result = "'" + text + "'";
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment