Skip to content

MirzaCryptoHelpers.Extensions

Mirza Ghulam Rasyid edited this page Oct 6, 2017 · 3 revisions

MirzaCryptoHelpers.Extensions Namespace

This namespace contains many implementations from MirzaCryptoHelpers.Common namespace that is being turned to extension methods. There are Five classes for 5 datatypes: string, char, short, int, long. And the methods work just the same as you find in MirzaCryptoHelpers.Common namespace. And if conversion fails, it'll return null/empty.

Add namespace first

using MirzaCryptoHelpers.Extensions;

ToUTF8Bytes

"hello".ToUTF8Bytes(); //return byte[5] { 104, 101, 108, 108, 111 }
1.ToUTF8Bytes(); //return byte[1] { 49 } not 1 because it'll be conveted to UTF-8 encoding
'a'.ToUTF8Bytes(); //return byte[1] { 97 } 

ToBinary And FromBinary

"hello".ToBinary(); //return 1101000 1100101 1101100 1101100 1101111
"1101000 1100101 1101100 1101100 1101111".FromBinary(); //return hello
//char, int, short, int and long only support ToBinary()
//short, int and long will return exact value to binary.
//ex: 20 will return 10100 not ASCII/UTF-8 version 110010 110000

ToOctal And FromOctal

"hello".ToOctal(); //return 150 145 154 154 157
"150 145 154 154 157".FromOctal() //return hello

ToHexadecimal And FromHexadecimal

"hello".ToHexadecimal(); //return 68 65 6c 6c 6f
"68 65 6c 6c 6f".FromBinary(); //return hello

GetMD5Hash, GetSHA1Hash, GetSHA256Hash, GetSHA384Hash And GetSHA512Hash

"i am string".GetMD5Hash(); //you can use GetSHA1Hash, GetSHA256Hash, GetSHA384Hash And GetSHA512Hash too!
'z'.GetSHA1Hash();
231.GetSHA256Hash(); 

ToBase64String And FromBase64String

"hello".ToBase64String(); //return aGVsbG8=
"aGVsbG8=".FromBase64String(); return hello