샘플 C

  • 릴리스 버전: Xanadu
  • 업데이트 날짜 2024년 08월 01일
  • 읽기2분
  • 이 C 클래스는 세 개의 입력 매개 변수에서 다이제스트 토큰을 만드는 방법을 보여 줍니다.

    • strEncryptionMethod – 해시 계산 방법(SHA1, SHA256 또는 MD5)을 나열합니다.
    • message – 다이제스트 토큰으로 변환할 값을 나열합니다.
    • sharedKey – 비밀 키를 나열합니다.
    주:
    다이제스트 토큰 생성을 위해 SHA256과 같은 더 강력한 보안 해시 알고리즘을 사용합니다.
    이 샘플에서는 다음을 가정합니다.
    • 웹 서버는 C를 지원합니다.
    • 다른 코드는 이 클래스를 호출하고 예상 매개 변수를 전달합니다

    샘플 코드

      private stringdigestData(string strEncryptionMethod , string message , string sharedKey ) {
                UnicodeEncoding myUnicodeEncoding  = new UnicodeEncoding ( ) ;
     
                byte [ ] messageBytes  = System. Text. Encoding. ASCII. GetBytes (message ) ;
                byte [ ] sharedKeyBytes  = System. Text. Encoding. ASCII. GetBytes (sharedKey ) ;
                byte [ ] hashedMessage ;
     
                string b64SHA1Message ;
     
                 if (this. DEBUG ) {
                    TextBoxMessage. Text = message ;
                    TextBoxSecret. Text = sharedKey ; }
     
                 switch ( (strEncryptionMethod ) )
     
                 { case "SHA1" :
     
                        HMACSHA1 hmacsha1  = new HMACSHA1 ( ) ;
                        hmacsha1. Key = sharedKeyBytes ;
                        hashedMessage  = hmacsha1. ComputeHash (messageBytes ) ;
                        b64SHA1Message  = Convert. ToBase64String (hashedMessage ) ; if (this. DEBUG ) TextBoxDigest. Text = Convert. ToString (hashedMessage ) ; break ;
     
                     case "MD5" :
     
                        HMACMD5 hmacmd5  = new HMACMD5 (sharedKeyBytes ) ;
                        hashedMessage  = hmacmd5. ComputeHash (messageBytes ) ;
                        b64SHA1Message  = Convert. ToBase64String (hashedMessage ) ; if (this. DEBUG ) TextBoxDigest. Text = Convert. ToString (hashedMessage ) ; break ;
     
                     default :
     
                        b64SHA1Message  = "Unknown Encryption Method" ; break ;
     
                 }
     
     
                TextBoxBase64. Text = b64SHA1Message ; return b64SHA1Message ;
     
             }