星期三, 11月 08, 2023

2009-12-07 如何產生 SHA1 的檢查碼?


如何產生 SHA1 的檢查碼?

由於一般計算 MD5 或 SHA1 檢查碼均是以 ASCII 型態計算,要於 AS/400 EBCDIC 編碼下計算,均須呼叫轉碼
 API QTQCVRT 先將 AS/400 EBCDIC 轉換為 ASCII 編碼,再以 ASCII 值取得檢查碼。
SHA1 檢查碼 20 byte 長,轉換為 16 進位顯示為 40  位長。
MD5  檢查碼 16 byte 長,轉換為 16 進位顯示為 32  位長。


File  : QRPGLESRC
Member: SHA1R
Type  : RPGLE
OS version: 
Usage : CRTRPGBND (xxx/SHA1R)

      * The SHA1 Algorithm
      * http://php.net/manual/en/function.sha1.php
      * ascii 'apple'
      * SHA1= d0be2dc421be4fcd0172e5afceea3970e2f3d940

     H DFTACTGRP(*NO) ACTGRP('QILE') BNDDIR('QC2LE')
     DCipher           PR                  EXTPROC('_CIPHER')
     D                                 *   VALUE
     D                                 *   VALUE
     D                                 *   VALUE
     DConvert          PR                  EXTPROC('_XLATEB')
     D                                 *   VALUE
     D                                 *   VALUE
     D                               10u 0 VALUE
     Dcvthc            PR                  EXTPROC('cvthc')
     D                                1
     D                                1
     D                               10i 0 VALUE
     DControls         DS
     D Function                       5i 0 inz(5)
     D HashAlg                        1    inz(x'01')
     D Sequence                       1    inz(x'00')
     D DataLngth                     10i 0 inz(15)
     D Unused                         8    inz(*LOVAL)
     D HashCtxPtr                      *   inz(%addr(HashWorkArea))
     DHashWorkArea     S             96    inz(*LOVAL)
     DMsg              S             50
     DReceiverHex      S             20
     DReceiverPtr      S               *   inz(%addr(ReceiverHex))
     DReceiverChr      S             40
     DSourcePtr        S               *   inz(%addr(Msg))
     DStartMap         s            256
     DTo819            s            256
     DCCSID1           s             10i 0 inz(37)
     DST1              s             10i 0 inz(0)
     DL1               s             10i 0 inz(%size(StartMap))
     DCCSID2           s             10i 0 inz(819)
     DST2              s             10i 0 inz(0)
     DGCCASN           s             10i 0 inz(0)
     DL2               s             10i 0 inz(%size(To819))
     DL3               s             10i 0
     DL4               s             10i 0
     DFB               s             12
     D                 ds
     D x                              5i 0
     D LowX                    2      2
     D* Get all single byte ebcdic hex values
     C     0             do        255           x
     C                   eval      %subst(StartMap:x+1:1) = LowX
     C                   enddo
     C* Get conversion table for 819 from 37
     C                   call      'QTQCVRT'
     C                   parm                    CCSID1
     C                   parm                    ST1
     C                   parm                    StartMap
     C                   parm                    L1
     C                   parm                    CCSID2
     C                   parm                    ST2
     C                   parm                    GCCASN
     C                   parm                    L2
     C                   parm                    To819
     C                   parm                    L3
     C                   parm                    L4
     C                   parm                    FB
     C* Set message text
     C                   eval      Msg = 'apple'
     C                   eval      DataLngth = %len(%trimr(Msg))
     C* Now Change Msg to 819 from 37 using MI
     C                   callp     Convert( %addr(Msg)
     C                                     :%addr(To819)
     C                                     :%size(Msg))
     C* Get MD5 for Msg
     C                   callp     Cipher( %addr(ReceiverPtr)
     C                                    :%addr(Controls)
     C                                    :%addr(SourcePtr))
     C* Convert nibbles to characters
     C                   callp     cvthc( ReceiverChr
     C                                   :ReceiverHex
     C                                   :%size(ReceiverChr))
     C* Display the ascii "apple"
      * SHA1= d0be2dc421be4fcd0172e5afceea3970e2f3d940
     C     ReceiverChr   dsply
     C                   eval      *INLR = '1'
     C                   return








沒有留言: