Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
65
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Section 148.3: MD5

Hash functions map binary strings of an arbitrary length to small binary strings of a fixed length.

The MD5 algorithm is a widely used hash function producing a 128-bit hash value (16 Bytes, 32 Hexdecimal characters).

The ComputeHash method of the System.Security.Cryptography.MD5 class returns the hash as an array of 16

bytes.

Example:

using System;

using System.Security.Cryptography; using System.Text;

internal class Program

{

private static void Main()

{

var source = "Hello World!";

// Creates an instance of the default implementation of the MD5 hash algorithm. using (var md5Hash = MD5.Create())

{

// Byte array representation of source string

var sourceBytes = Encoding.UTF8.GetBytes(source);

//Generate hash value(Byte Array) for input data var hashBytes = md5Hash.ComputeHash(sourceBytes);

//Convert hash byte array to string

var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);

// Output the MD5 hash

Console.WriteLine("The MD5 hash of " + source + " is: " + hash);

}

}

}

Output: The MD5 hash of Hello World! is: ED076287532E86365E841E92BFC50D8C

Security Issues:

Like most hash functions, MD5 is neither encryption nor encoding. It can be reversed by brute-force attack and su ers from extensive vulnerabilities against collision and preimage attacks.

Section 148.4: SHA1

using System;

using System.Security.Cryptography; using System.Text;

namespace ConsoleApplication1

{

class Program

{

GoalKicker.com – C# Notes for Professionals

711

static void Main(string[] args)

{

string source = "Hello World!";

using (SHA1 sha1Hash = SHA1.Create())

{

//From String to byte array

byte[] sourceBytes = Encoding.UTF8.GetBytes(source); byte[] hashBytes = sha1Hash.ComputeHash(sourceBytes);

string hash = BitConverter.ToString(hashBytes).Replace("-",String.Empty);

Console.WriteLine("The SHA1 hash of " + source + " is: " + hash);

}

}

}

}

Output:

The SHA1 hash of Hello Word! is: 2EF7BDE608CE5404E97D5F042F95F89F1C232871

Section 148.5: SHA256

using System;

using System.Security.Cryptography; using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string source = "Hello World!";

using (SHA256 sha256Hash = SHA256.Create())

{

//From String to byte array

byte[] sourceBytes = Encoding.UTF8.GetBytes(source); byte[] hashBytes = sha256Hash.ComputeHash(sourceBytes);

string hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

Console.WriteLine("The SHA256 hash of " + source + " is: " + hash);

}

}

}

}

Output:

The SHA256 hash of Hello World! is: 7F83B1657FF1FC53B92DC18148A1D65DFC2D4B1FA3D677284ADDD200126D9069

Section 148.6: SHA384

using System;

using System.Security.Cryptography; using System.Text;

namespace ConsoleApplication1

{

GoalKicker.com – C# Notes for Professionals

712

class Program

{

static void Main(string[] args)

{

string source = "Hello World!";

using (SHA384 sha384Hash = SHA384.Create())

{

//From String to byte array

byte[] sourceBytes = Encoding.UTF8.GetBytes(source); byte[] hashBytes = sha384Hash.ComputeHash(sourceBytes);

string hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

Console.WriteLine("The SHA384 hash of " + source + " is: " + hash);

}

}

}

}

Output:

The SHA384 hash of Hello World! is: BFD76C0EBBD006FEE583410547C1887B0292BE76D582D96C242D2A792723E3FD6FD061F9D5CFD13B8F961358E6A DBA4A

Section 148.7: SHA512

using System;

using System.Security.Cryptography; using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string source = "Hello World!";

using (SHA512 sha512Hash = SHA512.Create())

{

//From String to byte array

byte[] sourceBytes = Encoding.UTF8.GetBytes(source); byte[] hashBytes = sha512Hash.ComputeHash(sourceBytes);

string hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

Console.WriteLine("The SHA512 hash of " + source + " is: " + hash);

}

}

}

}

Output: The SHA512 hash of Hello World! is: 861844D6704E8573FEC34D967E20BCFEF3D424CF48BE04E6DC08F2BD58C729743371015EAD891CC3CF1C9D34B49 264B510751B1FF9E537937BC46B5D6FF4ECC8

GoalKicker.com – C# Notes for Professionals

713

Chapter 149: Generating Random Numbers

in C#

Parameters

Seed

minValue maxValue return value

Details

A value for generating random numbers. If not set, the default value is determined by the current system time.

Generated numbers won't be smaller than this value. If not set, the default value is 0.

Generated numbers will be smaller than this value. If not set, the default value is Int32.MaxValue. Returns a number with random value.

Section 149.1: Generate a random int

This example generates random values between 0 and 2147483647.

Random rnd = new Random();

int randomNumber = rnd.Next();

Section 149.2: Generate a random int in a given range

Generate a random number between minValue and maxValue - 1.

Random rnd = new Random();

var randomBetween10And20 = rnd.Next(10, 20);

Section 149.3: Generating the same sequence of random numbers over and over again

When creating Random instances with the same seed, the same numbers will be generated.

int seed = 5;

for (int i = 0; i < 2; i++)

{

Console.WriteLine("Random instance " + i); Random rnd = new Random(seed);

for (int j = 0; j < 5; j++)

{

Console.Write(rnd.Next()); Console.Write(" ");

}

Console.WriteLine();

}

Output:

Random

instance

0

 

 

726643700

610783965

564707973

1342984399 995276750

Random

instance

1

 

 

726643700

610783965

564707973

1342984399 995276750

 

 

 

 

 

 

GoalKicker.com – C# Notes for Professionals

714