midrange.com code scratchpad
Name:
MR9000HASH/MR9000HINT
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
01/22/2018 09:22:24 pm
IP:
Logged
Description:
MR9000HASH constructs an MD5 hash for the first (file Length - 16 bytes) bytes in a record, and places it in the last 16bytes
MR9000HINT reconstructs the hash, and prints all records with an inconsistency.
Code:
  1. We have implemented an application framework utilizing MVC patterns. We are using optimistic locking. What this means
  2. is for the original read of a record (via SQL, loading a subfile) the record is not locked. If the record is changed, 
  3. we chain (RLA for transaction processing) with a lock. If we were using a timestamp, we would see if it had changed, 
  4. and if it has, we display a screen allowing the user to reload the record or force their change on top of the existing
  5. record. Rather than using a timestamp, we are using a 16 byte MD5 hash. This has a free benefit, in that we can
  6. now test the record for integrity. If someone changes the record outside the framework (DFU, ftp, etc.) and does
  7. not explicitly rebuild the hash, then we have an indentifiable integrity issue. While not foolproof, most hackers 
  8. are not going to be sophisticated enough to rebuild the MD5 hash.
  9.  
  10. The standard across the framework is the last 16 bytes of each file is the hash. MR9000HASH will update the hash
  11. for every record in a file (just OVRDBF INPUT tofile), and MR9000HINT will print every record for which the hash
  12. is out of whack.
  13.  
  14.  
  15.  
  16.       //-------------------------------------------------------------------------------------------------------------//
  17.       //                                                                                                             //
  18.       //                                                                                                             //
  19.       //                                                   MR9000HASH                                                //
  20.       //                                                                                                             //
  21.       //                                                                                                             //
  22.       //                                        create cryptographic checksum                                        //
  23.       //                                                                                                             //
  24.       //                                                                                                             //
  25.       //-------------------------------------------------------------------------------------------------------------//
  26.        Ctl-Opt dftActGrp(*No) actGrp(*Caller)
  27.                option(*SrcStmt:*NoDebugIO) debug(*Yes)
  28.                bndDir('QUSAPIBD')
  29.                main(af9000hash)                                                ;
  30.       //-------------------------------------------------------------------------------------------------------------//
  31.       //                                                                                                             //
  32.       //... files ...                                                                                                //
  33.       //                                                                                                             //
  34.       //-------------------------------------------------------------------------------------------------------------//
  35.        Dcl-F input Disk(1024) Usage(*INPUT:*UPDATE) InfDS(fileInfo) UsrOpn     ;
  36.       //-------------------------------------------------------------------------------------------------------------//
  37.       //                                                                                                             //
  38.       // ... procedure interfaces ...                                                                                //
  39.       //                                                                                                             //
  40.       //-------------------------------------------------------------------------------------------------------------//
  41.       //
  42.       //... services (external) ...
  43.       //
  44.        Dcl-PR calcMD5       ExtProc('Qc3CalculateHash')                        ;
  45.          *n                 Char(1)         Const           Options(*VarSize)  ;
  46.          *n                 Uns(10)         Const                              ;
  47.          *n                 Char(8)         Const                              ;
  48.          *n                 Char(1)         Const           Options(*VarSize)  ;
  49.          *n                 Char(8)         Const                              ;
  50.          *n                 Char(1)         Const                              ;
  51.          *n                 Char(10)        Const                              ;
  52.          *n                 Char(1)                         Options(*VarSize)  ;
  53.          *n                 Char(256)                                          ;
  54.        End-PR                                                                  ;
  55.       //-------------------------------------------------------------------------------------------------------------//
  56.       //                                                                                                             //
  57.       // ... standalone variables ...                                                                                //
  58.       //                                                                                                             //
  59.       //-------------------------------------------------------------------------------------------------------------//
  60.        Dcl-DS fileInfo      Len(528)                                           ;
  61.          fileLength         Int(5)          Pos(125)                           ;
  62.        End-DS                                                                  ;
  63.  
  64.        Dcl-DS buffer        Len(1024)                       End-DS             ;
  65.  
  66.        Dcl-DS errDS                                                            ;
  67.          *n                 Uns(10)         Inz(%Len(errDS))                   ;
  68.          errAvl             Uns(10)                                            ;
  69.          *n                 Char(7)                                            ;
  70.          *n                 Char(1)                                            ;
  71.          *n                 Char(240)                                          ;
  72.        End-DS                                                                  ;
  73.  
  74.        Dcl-DS algd0500                                                         ;
  75.          algName            Int(10)         Inz(1)                             ;
  76.        End-DS                                                                  ;
  77.  
  78.        Dcl-S hash               Char(16)                                       ;
  79.       //-------------------------------------------------------------------------------------------------------------//
  80.       //                                                                                                             //
  81.       //                                                 Procedures                                                  //
  82.       //                                                                                                             //
  83.       //-------------------------------------------------------------------------------------------------------------//
  84.       //                                                  Mainline                                                   //
  85.       //-------------------------------------------------------------------------------------------------------------//
  86.        Dcl-Proc af9000hash                                                     ;
  87.  
  88.         init();
  89.         DoW ( reader() )                                                       ;
  90.           process()                                                            ;
  91.         EndDo                                                                  ;
  92.         eoj()                                                                  ;
  93.         Return                                                                 ;
  94.  
  95.        End-Proc                                                                ;
  96.       //-------------------------------------------------------------------------------------------------------------//
  97.       // reader                                                                                                      //
  98.       //-------------------------------------------------------------------------------------------------------------//
  99.        Dcl-Proc reader                                                         ;
  100.           Dcl-PI *n         Ind                             End-PI             ;
  101.  
  102.         Read input buffer                                                      ;
  103.         Return ( Not %Eof(input) )                                             ;
  104.  
  105.        End-Proc                                                                ;
  106.       //-------------------------------------------------------------------------------------------------------------//
  107.       // process                                                                                                     //
  108.       //-------------------------------------------------------------------------------------------------------------//
  109.        Dcl-Proc process                                                        ;
  110.  
  111.         calcMD5( buffer                                                         
  112.                : fileLength - 16                                                
  113.                : 'DATA0100'                                                     
  114.                : algd0500                                                       
  115.                : 'ALGD0500'                                                     
  116.                : '0'
  117.                : *Blanks
  118.                : hash
  119.                : errDS
  120.                )                                                               ;
  121.  
  122.         %SubSt(buffer:fileLength - 15:16) = hash                               ;
  123.         Update input buffer                                                    ;
  124.         Return                                                                 ;
  125.  
  126.        End-Proc                                                                ;
  127.       //-------------------------------------------------------------------------------------------------------------//
  128.       // init                                                                                                        //
  129.       //-------------------------------------------------------------------------------------------------------------//
  130.        Dcl-Proc init                                                           ;
  131.  
  132.         Open input                                                             ;
  133.         Return                                                                 ;
  134.  
  135.        End-Proc                                                                ;
  136.       //-------------------------------------------------------------------------------------------------------------//
  137.       // eoj                                                                                                         //
  138.       //-------------------------------------------------------------------------------------------------------------//
  139.        Dcl-Proc eoj                                                            ;
  140.  
  141.         Close input                                                            ;// uninsightful comment
  142.         Return                                                                 ;
  143.  
  144.        End-Proc                                                                ;
  145.       //-------------------------------------------------------------------------------------------------------------//
  146.       //                                                                                                             //
  147.       //                                                                                                             //
  148.       //                                                   MR9000HINT                                                //
  149.       //                                                                                                             //
  150.       //                                                                                                             //
  151.       //                                        verify checksum intergrity                                           //
  152.       //                                                                                                             //
  153.       //                                                                                                             //
  154.       //-------------------------------------------------------------------------------------------------------------//
  155.       //-------------------------------------------------------------------------------------------------------------//
  156.        Ctl-Opt dftActGrp(*No) actGrp(*Caller)
  157.                option(*SrcStmt:*NoDebugIO) debug(*Yes)
  158.                bndDir('QUSAPIBD')
  159.                main(af9000hint)                                                ;
  160.       //-------------------------------------------------------------------------------------------------------------//
  161.       //                                                                                                             //
  162.       //... files ...                                                                                                //
  163.       //                                                                                                             //
  164.       //-------------------------------------------------------------------------------------------------------------//
  165.        Dcl-F input   Disk(1024) Usage(*Input:*Update) InfDS(fileInfo) UsrOpn   ;
  166.        Dcl-F qsysprt Printer(132)                                     UsrOpn   ;
  167.       //-------------------------------------------------------------------------------------------------------------//
  168.       //                                                                                                             //
  169.       // ... procedure interfaces ...                                                                                //
  170.       //                                                                                                             //
  171.       //-------------------------------------------------------------------------------------------------------------//
  172.       //
  173.       //... services (external) ...
  174.       //
  175.        Dcl-PR calcMD5       ExtProc('Qc3CalculateHash')                        ;
  176.          *n                 Char(1)         Const           Options(*VarSize)  ;
  177.          *n                 Uns(10)         Const                              ;
  178.          *n                 Char(8)         Const                              ;
  179.          *n                 Char(1)         Const           Options(*VarSize)  ;
  180.          *n                 Char(8)         Const                              ;
  181.          *n                 Char(1)         Const                              ;
  182.          *n                 Char(10)        Const                              ;
  183.          *n                 Char(1)                         Options(*VarSize)  ;
  184.          *n                 Char(256)                                          ;
  185.        End-PR                                                                  ;
  186.       //-------------------------------------------------------------------------------------------------------------//
  187.       //                                                                                                             //
  188.       // ... standalone variables ...                                                                                //
  189.       //                                                                                                             //
  190.       //-------------------------------------------------------------------------------------------------------------//
  191.        Dcl-DS fileInfo      Len(528)                                           ;
  192.          fileLength         Int(5)          Pos(125)                           ;
  193.        End-DS                                                                  ;
  194.  
  195.        Dcl-DS buffer        Len(1024)                       End-DS             ;
  196.  
  197.        Dcl-DS errDS                                                            ;
  198.          *n                 Uns(10)         Inz(%Len(errDS))                   ;
  199.          errAvl             Uns(10)                                            ;
  200.          *n                 Char(7)                                            ;
  201.          *n                 Char(1)                                            ;
  202.          *n                 Char(240)                                          ;
  203.        End-DS                                                                  ;
  204.  
  205.        Dcl-DS algd0500                                                         ;
  206.          algName            Int(10)         Inz(1)                             ;
  207.        End-DS                                                                  ;
  208.  
  209.        Dcl-S hash               Char(16)                                       ;
  210.        Dcl-S rptLength          Like(fileLength)                               ;
  211.        Dcl-S output             Char(132)                                      ;
  212.       //-------------------------------------------------------------------------------------------------------------//
  213.       //                                                                                                             //
  214.       // ... 'O' specs (I still use them) ...                                                                        //
  215.       //                                                                                                             //
  216.       //-------------------------------------------------------------------------------------------------------------//
  217.      oqsysprt   e                        1
  218.      o                       output             132
  219.       //-------------------------------------------------------------------------------------------------------------//
  220.       //                                                                                                             //
  221.       //                                                 Procedures                                                  //
  222.       //                                                                                                             //
  223.       //-------------------------------------------------------------------------------------------------------------//
  224.       //                                                  Mainline                                                   //
  225.       //-------------------------------------------------------------------------------------------------------------//
  226.        Dcl-Proc af9000hint                                                     ;
  227.  
  228.         init()                                                                 ;
  229.         DoW ( reader() )                                                       ;
  230.           process()                                                            ;
  231.         EndDo                                                                  ;
  232.         eoj()                                                                  ;
  233.         Return                                                                 ;
  234.  
  235.        End-Proc                                                                ;
  236.       //-------------------------------------------------------------------------------------------------------------//
  237.       // reader                                                                                                      //
  238.       //-------------------------------------------------------------------------------------------------------------//
  239.        Dcl-Proc reader                                                         ;
  240.           Dcl-PI *n         Ind                             End-PI             ;
  241.  
  242.         Read input buffer                                                      ;
  243.         Return ( Not %Eof(input) )                                             ;
  244.  
  245.        End-Proc                                                                ;
  246.       //-------------------------------------------------------------------------------------------------------------//
  247.       // process                                                                                                     //
  248.       //-------------------------------------------------------------------------------------------------------------//
  249.        Dcl-Proc process                                                        ;
  250.  
  251.        calcMD5( buffer
  252.             : fileLength - 16
  253.             : 'DATA0100'
  254.             : algd0500
  255.             : 'ALGD0500'
  256.             : '0'
  257.             : *Blanks
  258.             : hash
  259.             : errDS
  260.             )                                                                  ;
  261.  
  262.        If ( %SubSt(buffer:fileLength - 15:16) <> hash )                        ;
  263.          output = %SubSt(buffer:1:rptLength)                                   ;
  264.          Except                                                                ;
  265.        EndIf                                                                   ;
  266.  
  267.        Return                                                                  ;
  268.        End-Proc                                                                ;
  269.       //-------------------------------------------------------------------------------------------------------------//
  270.       // init                                                                                                        //
  271.       //-------------------------------------------------------------------------------------------------------------//
  272.        Dcl-Proc init                                                           ;
  273.  
  274.        Open input                                                              ;
  275.        Open qsysprt                                                            ;
  276.  
  277.        If ( fileLength > 148 )                                                 ; 
  278.          rptLength = 132                                                       ;
  279.        Else                                                                    ;
  280.          rptLength = fileLength - 16                                           ;
  281.        EndIf                                                                   ;
  282.  
  283.        Return                                                                  ;
  284.        End-Proc                                                                ;
  285.       //-------------------------------------------------------------------------------------------------------------//
  286.       // eoj                                                                                                         //
  287.       //-------------------------------------------------------------------------------------------------------------//
  288.        Dcl-Proc eoj                                                            ;
  289.  
  290.        Close input                                                             ;// uninsightful comment
  291.        Close qsysprt                                                           ;
  292.        Return                                                                  ;
  293.  
  294.        End-Proc                                                                ;
  295.  
© 2004-2019 by midrange.com generated in 0.009s valid xhtml & css