× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



For your reference.
Command GENHASH -- Caculate Hash with Qc3CalculateHash API
Generate hash with hash algorithm
MD5 16 bytes
SHA-1 20 bytes
SHA-256 32 bytes
SHA-384 48 bytes
SHA-512 64 bytes

Command GENHASH
CPP GENHASHC
RPGLE GENHASHR

/*-------------------------------------------------------------------*/
/* */
/* Program . . : GenHashC */
/* Description : Generate Hash */
/* Author . . : Vengoal Chang */
/* Published . : AS400ePaper */
/* Date . . . : October 6,2022 */
/* */
/* Program function: GENHASH command processing program */
/* */
/* */
/* Program summary */
/* --------------- */
/* */
/* Compile options: */
/* CrtClPgm Pgm( GENHASHC ) */
/* SrcFile( QCLSRC ) */
/* SrcMbr( *PGM ) */
/* */
/*-------------------------------------------------------------------*/
Pgm ( &DataIn +
&DataLen +
&HashAlg +
&Md5 +
&Sha1 +
&Sha256 +
&Sha384 +
&Sha512 +
&HashHex +
)

Dcl &DataIn *Char 4098 /* VARYING LENGTH */
Dcl &DataHash *Char 4096
Dcl &DataLen *Int
Dcl &HashAlg *Int
Dcl &Md5 *Char 16
Dcl &Sha1 *Char 20
Dcl &Sha256 *Char 32
Dcl &Sha384 *Char 48
Dcl &Sha512 *Char 64
Dcl &Hash64 *Char 64
Dcl &HashHex *Char 128
Dcl &Algorithm *Char 7

MonMsg CPF0000 *N GoTo Error

If ( &DataLen = 0 ) Do
ChgVar &DataLen %Bin( &DataIn 1 2 )
EndDo

ChgVar &DataHash %Sst( &DataIn 3 &DataLen )

Call GenHashR ( &DataHash +
&DataLen +
&HashAlg +
&Hash64 +
&HashHex +
)

Select
When ( &HashAlg = 1 ) Do
ChgVar &Md5 %Sst( &Hash64 1 16)
ChgVar &Algorithm 'MD5'
EndDo
When ( &HashAlg = 2 ) Do
ChgVar &Sha1 %Sst( &Hash64 1 20)
ChgVar &Algorithm 'SHA-1'
EndDo
When ( &HashAlg = 3 ) Do
ChgVar &Sha256 %Sst( &Hash64 1 32)
ChgVar &Algorithm 'SHA-256'
EndDo
When ( &HashAlg = 4 ) Do
ChgVar &Sha384 %Sst( &Hash64 1 48)
ChgVar &Algorithm 'SHA-384'
EndDo
When ( &HashAlg = 5 ) Do
ChgVar &Sha512 &Hash64
ChgVar &Algorithm 'SHA-512'
EndDo
EndSelect

SndPgmMsg MsgId( CPF9898 ) +
MsgF( QCPFMSG ) +
MsgDta( &Algorithm *Bcat 'hash :' *Bcat &HashHex ) +
ToPgmQ( *Ext ) +
MsgType( *Comp )

Return

Error:
Call QMHMOVPM ( ' ' +
'*DIAG' +
x'00000001' +
'*PGMBDY' +
x'00000001' +
x'0000000800000000' +
)

Call QMHRSNEM ( ' ' +
x'0000000800000000' +
)

EndPgm:
EndPgm
**
** Program . . : GenHashR
** Description : Generate Hash
** Author . . : Vengoal Chang
** Published . : AS400ePaper
** Date . . . : October 06, 2022
**
**
**
** Programmer's notes:
** Cryptographic Services APIs
** Qc3CalculateHash Calculate Hash
**
**
**
** Compile options required:
** CrtBndRpg Pgm( GENHASHR )
** SrcMbr( GENHASHR )
**
**-- Header specifications: --------------------------------------------**
h DftActGrp(*NO) ActGrp('CRYPTO') BndDir('QC2LE') Debug
h Option(*SRCSTMT:*NODEBUGIO)

d main PR ExtPgm('GENHASHR')
d InputData 4096
d InputDataLen 10i 0
d HashAlg 10i 0
d Hash 64
d HashHex 128
d main PI
d InputData 4096
d InputDataLen 10i 0
d HashAlg 10i 0
d Hash 64
d HashHex 128

d Qc3CalculateHash...
d PR ExtProc('Qc3CalculateHash')
d InData * value
d IndataL 10i 0 const
d InDataF 8a const
d AlgoDes 16a const
d AlgoFmt 8a const
d CryptoSP 1a const
d CryptoDev 1a const options(*omit)
d Hash 64a options(*varsize:*omit)
d ErrorCode 32767a options(*varsize)

d ALGD0500_t ds qualified
d based(Template)
d HashAlg 10i 0

d QDCXLATE PR ExtPgm('QDCXLATE')
d len 5p 0 const
d data 32702a options(*varsize)
d table 10a const

d iconv_t DS qualified
d based(StructureTemplate)
d return_value 10I 0
d cd 10I 0 dim(12)

d QtqCode_T DS qualified
d CCSID 10I 0 inz
d ConvAlt 10I 0 inz
d SubsAlt 10I 0 inz
d ShiftAlt 10I 0 inz
d InpLenOp 10I 0 inz
d ErrorOpt 10I 0 inz
d Reserved 8A inz(*ALLx'00')

d iconv_code_t DS qualified
d owner 8a inz('IBMCCSID')
d CCSID 5a inz('00000')
D ReservedTo
d ConvAlt 3a inz('000')
D overlay(ReservedTo)
d SubsAlt 1a inz('0')
D overlay(ReservedTo:*next)
d ShiftAlt 1a inz('0')
D overlay(ReservedTo:*next)
d InpLenOpt 1a inz('0')
D overlay(ReservedTo:*next)
d ErrorOpt 1a inz('0')
D overlay(ReservedTo:*next)
d ReservedFrom 12A inz(*ALLx'00')
D overlay(ReservedTo:*next)

d QtqIconvOpen PR extproc('QtqIconvOpen')
d like(iconv_t)
d toCode likeds(QtqCode_t) const
d fromCode likeds(QtqCode_t) const

D iconv_open PR extproc('iconv_open')
D like(iconv_t)
D toCode like(iconv_code_t) const
D fromCode like(iconv_code_t) const

d iconv PR 10U 0 extproc('iconv')
d cd like(iconv_t) value
d inbuf *
d inbytesleft 10U 0
d outbuf *
d outbytesleft 10U 0

d QlgTransformUCSData...
d PR 10U 0 extproc('QlgTransformUCSData')
d xformtype 10i 0 value
d inbuf *
d inbytesleft 10U 0
d outbuf *
d outbytesleft 10U 0
d outspacereq 10U 0

D ICONV_FAIL C CONST(4294967295)

D iconv_close PR 10I 0 extproc('iconv_close')
D cd like(iconv_t) value
*----------------------------------------
d source ds likeds(QtqCode_t)
d inz(*likeds)
d target ds likeds(QtqCode_t)
d inz(*likeds)
d toEBC ds likeds(iconv_t)

D p_input s *
D p_output s *
D inputleft s 10u 0
D outputleft s 10u 0

d cvthc PR ExtProc('cvthc')
d target 65534A options(*varsize)
d src_bits 32767A options(*varsize) const
d tgt_length 10I 0 value

d ErrorNull ds qualified
d BytesPro 10i 0 inz(0)
d BytesAvai 10i 0 inz(0)

d HASH_MD5 c 1
d HASH_SHA1 c 2
d HASH_SHA256 c 3
d HASH_SHA384 c 4
d HASH_SHA512 c 5

d MD5Hex s 32
d SHA1Hex s 40
d SHA256Hex s 64
d SHA384Hex s 96
d SHA512Hex s 128

d data s 4096A
d len s 10u 0
d DataToHash s 4096A
d DataLen s 10u 0
D outputbuf s 4096a
D outputlen s 10u 0
d alg ds likeds(ALGD0500_t)
d binHash s 64a
d $hex s 128a

/free

// when InputData defined as varying use following:
// data = InputData;
// len = %len(InputData);

data = %Subst(InputData : 1 : InputDataLen);
len = InputDataLen;

//Set the HASH Algorithm you want to use !
alg.HashAlg = HashAlg ;

//Convert from EBCDIC to ASCII (skip this step if you want Hash in EBCDIC)
// QDCXLATE(len: data: 'QTCPASC');

// set conversion from 937 to 950
// -----------------------------------------------

source.CCSID = 937;
target.CCSID = 950;
toEBC = QtqIconvOpen( target: source );

if (toEBC.return_value = -1);
// handle error...
endif;

// -----------------------------------------------
// Translate data.
//
// the iconv() API will increment/decrement
// the pointers and lengths, so make sure you
// do not use the original pointers...
// -----------------------------------------------

p_input = %addr(data);
inputleft = len;

p_output = %addr(outputbuf);
outputleft = %size(outputbuf);

iconv( toEBC
: p_input
: inputleft
: p_output
: outputleft );

// -----------------------------------------------
// if needed, you can calculate the length of
// the decoded data by subtracting the amount
// of space left in the buffer from the total
// buffer size.
//
// At this point, 'outputbuf' should contain
// the EBCDIC data.
// -----------------------------------------------

outputlen = %size(outputbuf) - outputleft;
DataToHash = %subst(outputbuf:1:outputlen);
DataLen = outputlen;
//dump;

// -----------------------------------------------
// when you are completely done, call iconv_close()
// to free up memory.
// -----------------------------------------------
iconv_close(toEBC);

//API to calculate the SHA1 hash
Qc3CalculateHash( %addr(DataToHash)
: DataLen
: 'DATA0100'
: alg
: 'ALGD0500'
: '0'
: *OMIT
: binHash
: ErrorNull );

//Convert to HEX
cvthc( $hex: binHash : %len(binHash)*2);
select;
when alg.HashAlg = HASH_MD5 ;
Hash = %SubSt( binHash : 1 : 16);
MD5Hex = %SubSt( $hex : 1 : 32);
HashHex = MD5Hex;
when alg.HashAlg = HASH_SHA1;
Hash = %SubSt( binHash : 1 : 20);
SHA1Hex = %SubSt( $hex : 1 : 40);
HashHex = SHA1Hex;
when alg.HashAlg = HASH_SHA256;
Hash = %SubSt( binHash : 1 : 32);
SHA256Hex = %SubSt( $hex : 1 : 64);
HashHex = SHA256Hex;
when alg.HashAlg = HASH_SHA384;
Hash = %SubSt( binHash : 1 : 48);
SHA384Hex = %SubSt( $hex : 1 : 96);
HashHex = SHA384Hex;
when alg.HashAlg = HASH_SHA512;
Hash = %SubSt( binHash : 1 : 64);
SHA512Hex = %SubSt( $hex : 1 : 128);
HashHex = SHA512Hex;
endsl;

//dump;
*inlr = *on;

/end-free

/*****************************************************************/
/* */
/* Command name: GenHash */
/* */
/* Author : Vengoal Chang */
/* */
/* Date written: 2022/10/06 */
/* */
/* Description : Generate hash with hash algorithm */
/* MD5 16 bytes */
/* SHA-1 20 bytes */
/* SHA-256 32 bytes */
/* SHA-384 48 bytes */
/* SHA-512 64 bytes */
/* */
/* To compile: */
/* CRTCMD CMD( GenHash ) */
/* PGM( GenHashC ) */
/* SRCMBR( GenHash ) */
/* ALLOW( *Ipgm *Bpgm ) */
/* */
/*****************************************************************/

Cmd Prompt('Generate data HASH')
Parm Kwd( DATA ) +
Type(*CHAR) +
Len(4096) +
Min(1) +
Expr(*YES) +
Vary(*YES *INT2) +
Prompt('Data to hash')

Parm Kwd( DTALEN ) +
Type(*INT4) +
Min(1) +
Expr(*YES) +
Prompt('Length of Data to hash')

Parm Kwd( HASHALG ) +
Type(*INT4) +
Rstd(*YES) +
SpcVal( (MD5 1) +
('SHA-1' 2) +
('SHA-256' 3) +
('SHA-384' 4) +
('SHA-512' 5) ) +
Min(1) +
Prompt('Hash algorithms')

Parm Kwd( MD5 ) +
Type(*CHAR) +
Len(16) +
RtnVal(*YES) +
PmtCtl(P0001) +
Prompt('CL var for MD5 HASH (16)')

Parm Kwd( SHA1 ) +
Type(*CHAR) +
Len(20) +
RtnVal(*YES) +
PmtCtl(P0002) +
Prompt('CL var for SHA-1 HASH (20)')

Parm Kwd( SHA256 ) +
Type(*CHAR) +
Len(20) +
RtnVal(*YES) +
PmtCtl(P0003) +
Prompt('CL var for SHA-256 HASH (32)')

Parm Kwd( SHA384 ) +
Type(*CHAR) +
Len(48) +
RtnVal(*YES) +
PmtCtl(P0004) +
Prompt('CL var for SHA-384 HASH (48)')

Parm Kwd( SHA512 ) +
Type(*CHAR) +
Len(64) +
RtnVal(*YES) +
PmtCtl(P0005) +
Prompt('CL var for SHA-512 HASH (64)')

Parm Kwd( HASHHEX ) +
Type(*CHAR) +
Len(128) +
RtnVal(*YES) +
Prompt('CL var for HASH Hex')

P0001: PmtCtl Ctl( HASHALG ) +
Cond(( *EQ 1 ))

P0002: PmtCtl Ctl( HASHALG ) +
Cond(( *EQ 2 ))

P0003: PmtCtl Ctl( HASHALG ) +
Cond(( *EQ 3 ))

P0004: PmtCtl Ctl( HASHALG ) +
Cond(( *EQ 4 ))

P0005: PmtCtl Ctl( HASHALG ) +
Cond(( *EQ 5 ))


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.