다음을 통해 공유


해시 생성

업데이트: 2007년 11월

관리되는 해시 클래스에서는 바이트 배열이나 관리되는 스트림 개체를 해시할 수 있습니다. 다음 예제에서는 SHA1 해시 알고리즘을 사용하여 문자열에 대한 해시 값을 만듭니다. 다음 예제에서는 UnicodeEncoding 클래스를 사용하여 문자열을 SHA1Managed 클래스를 사용하여 해시된 바이트 배열로 변환합니다. 그런 다음에는 해당 해시 값이 콘솔에 표시됩니다.

Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Module1
    Sub Main()
        Dim HashValue() As Byte

        Dim MessageString As String = "This is the original message!"

        'Create a new instance of the UnicodeEncoding class to 
        'convert the string into an array of Unicode bytes.
        Dim UE As New UnicodeEncoding()

        'Convert the string into an array of bytes.
        Dim MessageBytes As Byte() = UE.GetBytes(MessageString)

        'Create a new instance of the SHA1Managed class to create 
        'the hash value.
        Dim SHhash As New SHA1Managed()

        'Create the hash value from the array of bytes.
        HashValue = SHhash.ComputeHash(MessageBytes)

        'Display the hash value to the console. 
        Dim b As Byte
        For Each b In HashValue
            Console.Write("{0} ", b)
        Next b
    End Sub
End Module
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class Class1
{
   static void Main(string[] args)
   {
      byte[] HashValue;

      string MessageString = "This is the original message!";

      //Create a new instance of the UnicodeEncoding class to 
      //convert the string into an array of Unicode bytes.
      UnicodeEncoding UE = new UnicodeEncoding();

      //Convert the string into an array of bytes.
     byte[] MessageBytes = UE.GetBytes(MessageString);

      //Create a new instance of the SHA1Managed class to create 
      //the hash value.
      SHA1Managed SHhash = new SHA1Managed();

      //Create the hash value from the array of bytes.
      HashValue = SHhash.ComputeHash(MessageBytes);

      //Display the hash value to the console. 
      foreach(byte b in HashValue)
      {
         Console.Write("{0} ", b);
      }
   }
}

이 코드에서는 다음 문자열을 콘솔에 표시합니다.

59 4 248 102 77 97 142 201 210 12 224 93 25 41 100 197 213 134 130 135

참고 항목

개념

해시 확인

해시 코드를 사용하여 데이터 무결성 보장

기타 리소스

암호화 작업

암호화 서비스