<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to apply SHA256 hash to a column of a data set ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433563#M107500</link>
    <description>&lt;P&gt;Rick Langston wrote a paper &lt;A href="http://support.sas.com/resources/papers/proceedings16/SAS2800-2016.pdf" target="_self"&gt;that shows how to implement SHA256 in DATA step&lt;/A&gt;.&amp;nbsp; Not for the faint of heart, but you might find it useful.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Feb 2018 15:18:42 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2018-02-02T15:18:42Z</dc:date>
    <item>
      <title>How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433376#M107438</link>
      <description>&lt;P&gt;Hello Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The SAS script below extracts&amp;nbsp; 50 obs from a data set, then hashes with md5 2 cols of it, namely, first and last name and then calls a proc, that carries an implementation of SHA256, from a macro to hash the ssn column before it zips the final text file. The proc contains &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19924"&gt;@FriedEgg&lt;/a&gt;'s brain child and it lives in a file called SHA256AlgoCall.sas that's attached to this thread (well, we changed SHA1 to SHA256 in the proc but we claim no credit for the code development).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A submit block cannot be directly placed in a macro, hence the %Include of SHA256AlgoCall.sas inside macro fldValueSHA256. If the macro has been setup correctly and our logic to package proc groovy of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19924"&gt;@FriedEgg&lt;/a&gt; inside a separate sas file makes sense, we would like to know what's the syntax that calls the macro in the data step to successfully hash the ssn column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If our script logic to call the hash algorithm is wrong how should we think to make this happen ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We made great effort to reach this point and we're still in newbie aggressive learning mode.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Server is running SAS 9.3 on AIX 64, the client runs SAS 9.4 on Win10 x64 and the two are VPN connected&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We appreciate your time to help us.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*******************************************************************************************************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let dir1=%str(/new/dirct/loct/userLoc/logfiles/);&lt;BR /&gt;%let pgmname=%str(SchdDent_20170615);&lt;BR /&gt;%let date=&amp;amp;sysdate;&lt;BR /&gt;%let time=&amp;amp;systime;&lt;BR /&gt;%let ext1=%str(log);&lt;BR /&gt;%let break=%str(-);&lt;BR /&gt;%let final1=&amp;amp;dir1&amp;amp;pgmname&amp;amp;date&amp;amp;break&amp;amp;time..&amp;amp;ext1;&lt;BR /&gt;%put &amp;amp;final1;&lt;BR /&gt;&lt;BR /&gt;%let key1 = %nrstr(kd94hf93k423kf44&amp;amp;pfkkdhi9sl3r4s00) ;&lt;BR /&gt;&lt;BR /&gt;proc printto log="&amp;amp;final1";&lt;BR /&gt;run; quit;&lt;BR /&gt;&lt;BR /&gt;options nocenter ls=132 ps=32000 obs=50 compress=char;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;libname in3 '/new/dirct/loct/userLoc/dss20170615/';&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;*Macro to read in one month of Sched Dent;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;%macro readSchdDent(fy);&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;libname inscen&amp;amp;fy. "/mainDb/publs/SchdDent/";&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;data force schdDent&amp;amp;fy.;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;set inscen&amp;amp;fy..fy&amp;amp;fy.(keep = APPT_DT APPT_TIME SSN LASTNAME FIRSTNAME HIPAA_TAX1 HIPAA_TAX2 HIPAA_TAX3 HIPAA_TAX4 HIPAA_TAX5 SLOT_KEY FY FM);&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if FM='01';&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;run;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;proc append data = schdDent&amp;amp;fy. base = in3.schdDent_step1 (compress=char) force;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;run;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;proc datasets;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;delete schdDent&amp;amp;fy.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;run;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;%mend readSchdDent;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;%readSchdDent(16);&lt;BR /&gt;%readSchdDent(17);&lt;BR /&gt;&lt;BR /&gt;%macro fldValueSHA256 (key, fldVal);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%include "/new/dirct/loct/userLoc/SHA256AlgoCall.sas" /lrecl=700;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;run;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;amp;base64hmacsha256&lt;BR /&gt;%mend fldValueSHA256;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data in3.schdDent_step1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;modify in3.schdDent_step1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ssn = %fldValueSHA256 (key=key1, fldVal=ssn);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/*ssn = put(md5(upcase(trim(coalesce(ssn,''))) || '^' || '703447'),$hex32.)*/&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;lastname = put(md5(upcase(trim(coalesce(lastname,''))) || '^' || '703447'),$hex32.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;firstname = put(md5(upcase(trim(coalesce(firstname,''))) || '^' || '703447'),$hex32.);&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;filename out1 '/new/dirct/loct/userLoc/dss20170615/schdDent.txt';&lt;BR /&gt;&lt;BR /&gt;proc export data=in3.schdDent_step1&lt;BR /&gt;&amp;nbsp;&amp;nbsp; outfile=out1&lt;BR /&gt;&amp;nbsp;&amp;nbsp; dbms=dlm&lt;BR /&gt;&amp;nbsp;&amp;nbsp; replace;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; delimiter='|';&lt;BR /&gt;run;&lt;BR /&gt;/**/&lt;BR /&gt;/**compresses a file w/o encryption*/&lt;BR /&gt;/**creates a package.;*/&lt;BR /&gt;/*ods package(arch) open nopf;*/&lt;BR /&gt;/**/&lt;BR /&gt;/**adds the txt file to the package.;*/&lt;BR /&gt;/*ods package(arch) add file='/new/dirct/loct/userLoc/dss20170615/schdDent.txt';*/&lt;BR /&gt;/**/&lt;BR /&gt;/**compresses the package.;*/&lt;BR /&gt;/*ods package(arch) publish archive properties (archive_name='schdDent.zip' archive_path='/new/dirct/loct/userLoc/dss20170615');*/&lt;BR /&gt;/**/&lt;BR /&gt;/**closes the package.;*/&lt;BR /&gt;/*ods package(arch) close;*/&lt;BR /&gt;/**/&lt;BR /&gt;/**deletes the text file.;*/&lt;BR /&gt;/*data _null_;*/&lt;BR /&gt;/*&amp;nbsp;&amp;nbsp; &amp;nbsp;rc = fdelete('schdDent.txt');*/&lt;BR /&gt;/*run;*/&lt;BR /&gt;&lt;BR /&gt;/*compresses a file with encryption by calling pkzip (available in AIX) */&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;call system ('pkzipc -add -passphrase=$@asdfsdf!@@# /new/dirct/loct/userLoc/dss20170615/schdDent.zip /new/dirct/loct/userLoc/dss20170615/schdDent.txt');&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 19:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433376#M107438</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-02T19:36:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433414#M107448</link>
      <description>Perhaps I'm missing something here, but why aren't you using the institute supplied SHA256 function?&lt;BR /&gt;&lt;A href="http://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.3&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p04sqiymw1a6unn1uvh943eudcvz.htm&amp;amp;locale=en" target="_blank"&gt;http://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.3&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p04sqiymw1a6unn1uvh943eudcvz.htm&amp;amp;locale=en&lt;/A&gt;</description>
      <pubDate>Fri, 02 Feb 2018 07:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433414#M107448</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-02-02T07:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433521#M107485</link>
      <description>&lt;P&gt;Thanks for your prompt response LinusH.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please correct us if we're wrong on the following thought:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As mentioned on our original post the AIX server is running 9.3 version of SAS and the Win10 client has installed SAS 9.4; to be exact SAS EG 7.15 HF2 (7.100.5.6112) (64-bit) and&amp;nbsp; SAS for Windows TS Level 1M3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We're aware of the Institute's internally developed SHA256 , however, we were under the impression it'll be available only if the server side of SAS is on version 9.4.x, which isn't the case for us.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We experimented calling SHA256 running a short snippet of code against our AIX server from the URL you provided but we get the error shown below:&lt;/P&gt;&lt;P&gt;ERROR 68-185: The function SHA256 is unknown, or cannot be accessed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advice if we're missing something.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;S-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 19:38:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433521#M107485</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-02T19:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433563#M107500</link>
      <description>&lt;P&gt;Rick Langston wrote a paper &lt;A href="http://support.sas.com/resources/papers/proceedings16/SAS2800-2016.pdf" target="_self"&gt;that shows how to implement SHA256 in DATA step&lt;/A&gt;.&amp;nbsp; Not for the faint of heart, but you might find it useful.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 15:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433563#M107500</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2018-02-02T15:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433629#M107528</link>
      <description>&lt;P&gt;The main problem is that you have not built something with PROC GROOVY that you can call as a function from inside a data step.&amp;nbsp; It would also be good to note here that what you have implemented is HMAC SHA-256 like in my&amp;nbsp;&lt;A href="https://gist.github.com/FriedEgg/79ad315afa1b315e8ac3#file-base64hmacsha1-sas" target="_self"&gt;gist&lt;/A&gt;.&amp;nbsp; This is very different from SHA-256 and thus the built-in function in SAS 9.4,&amp;nbsp;&lt;A href="http://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p04sqiymw1a6unn1uvh943eudcvz.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;SHA256 function&lt;/A&gt;&amp;nbsp;recommended by&amp;nbsp;@LinusH doesn't apply and neither does the code from Rick on behalf of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;.&amp;nbsp; SAS 9.4 does however have a built-in function that duplicates this functionality, which is the&amp;nbsp;&lt;A href="http://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1tbs7458tyiwfn12ial1qnqo6l2.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;SHA256HMACHEX Function&lt;/A&gt;.&amp;nbsp; This all leads to the question...&amp;nbsp; Do you actually want HMAC SHA-256 or just SHA-256?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 18:03:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433629#M107528</guid>
      <dc:creator>FriedEgg</dc:creator>
      <dc:date>2018-02-02T18:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433672#M107541</link>
      <description>&lt;P&gt;Thanks for catching our error FriedEgg.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Our goal is the implementation of HMACSHA256 (changed the thread's Subject to reflect the right algorithm) instead of plain SHA256.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, the SAS server is running on version 9.3 (not 9.4), so, it is our understanding we can't use &lt;A href="http://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1tbs7458tyiwfn12ial1qnqo6l2.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self" rel="nofollow noopener noreferrer"&gt;SHA256HMACHEX Function&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you advice to call PROC GROOVY from a SAS Macro function which, in turn, we can use in the data step to hash the ssn column?&lt;/P&gt;&lt;P&gt;If not, how else would you recommend hashing the ssn?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate your input.&lt;/P&gt;&lt;P&gt;S-&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 19:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433672#M107541</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-02T19:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433703#M107553</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* writing this way to a file for ease of posting.  you would have it as a real file;&lt;BR /&gt;filename inc temp;
data _null_;
file inc;
input @;
put _infile_;
cards4;
filename cp temp;
proc groovy classpath=cp;

add sasjar="commons_codec" version="1.7.0.0_SAS_20121211183158"; *version is specific to SAS Installation and may differ from this;

submit parseonly;
 
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64

class SASHash { 
    String base64hmacsha256(String key, String message) {
        def mac = Mac.getInstance("HmacSHA256")
        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA256"))
        def hash = Base64.encodeBase64String(mac.doFinal(message.getBytes()))
        return hash
    }
}
 
endsubmit;
 
quit;

options set=classpath "%sysfunc(pathname(cp,f))";
;;;;
run;
&lt;BR /&gt;* general purpose macro can be expanded to include additional methods;
%macro SASHash(action,key=key,message=message,return=hash,include=inc);
%goto &amp;amp;action;

%compile:
%include &amp;amp;include;
%return;

%declare:
if _n_=1 then declare javaobj SASHash('SASHash');
%return;

%base64hmacsha256:
SASHash.callStringMethod("&amp;amp;action",strip(&amp;amp;key),strip(&amp;amp;message),&amp;amp;return);
%return;
%mend;

%let key = sdfoij3242039sdflkj3r23;
%SASHash(compile,include=inc)
data class;
    %SASHash(declare)
	length hashname $ 128;
	set sashelp.class;
	%SASHash(base64hmacsha256,key="&amp;amp;key",message=name,return=hashname)
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 21:26:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433703#M107553</guid>
      <dc:creator>FriedEgg</dc:creator>
      <dc:date>2018-02-02T21:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433768#M107571</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dir1=%str(/new/dirct/loct/userLoc/logfiles/);
%let pgmname=%str(SchdDent_20170615);
%let date=&amp;amp;sysdate;
%let time=&amp;amp;systime;
%let ext1=%str(log);
%let break=%str(-);
%let final1=&amp;amp;dir1&amp;amp;pgmname&amp;amp;date&amp;amp;break&amp;amp;time..&amp;amp;ext1;
%put &amp;amp;final1;

proc printto log="&amp;amp;final1";
run; quit;

options nocenter ls=132 ps=32000 obs=50 compress=char;        

    libname in3 '/new/dirct/loct/userLoc/dss20170615/';
 
    
*Macro to read in one month of Sched Dent;        
%macro readSchdDent(fy);        
    libname inscen&amp;amp;fy. "/mainDb/publs/SchdDent/";    

    data force schdDent&amp;amp;fy.;    

    set inscen&amp;amp;fy..fy&amp;amp;fy.(keep = APPT_DT APPT_TIME SSN LASTNAME FIRSTNAME HIPAA_TAX1 HIPAA_TAX2 HIPAA_TAX3 HIPAA_TAX4 HIPAA_TAX5 SLOT_KEY FY FM);    
            if FM='01';
    run;    

    proc append data = schdDent&amp;amp;fy. base = in3.schdDent_step1 (compress=char) force;    
    run;    

    proc datasets;        
        delete schdDent&amp;amp;fy.;
    run;    

%mend readSchdDent;        

%readSchdDent(16);
%readSchdDent(17);

data _null_;
	file inc;   /* extrnal file Data Step used to write output from a PUT statement*/
	input @;  /* holds input rcd for the next INPUT statemnt within the same iteration of the DATA step */
	put _infile_;  /* writes the last input data rcd read from the current input file or from data lines after DATELINES statenment */
	cards4;

	filename cp temp;
	proc groovy classpath=cp;

	add sasjar="commons_codec" version="1.4.0.0_SAS_20100917120335"; *version is specific to SAS Installation and may differ from this;

	submit parseonly;
	 
	import javax.crypto.Mac
	import javax.crypto.spec.SecretKeySpec
	import org.apache.commons.codec.binary.Base64

	class SASHash { 
	    String base64hmacsha256(String key, String message) {
	        def mac = Mac.getInstance("HmacSHA256")
	        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA256"))
	        def hash = Base64.encodeBase64String(mac.doFinal(message.getBytes()))
	        return hash
	    }
	}
	 
	endsubmit;
	 
	quit;

	options set=classpath "%sysfunc(pathname(cp,f))";
;;;;
run;

* general purpose macro can be expanded to include additional methods;
%macro SASHash(action,key=key,message=message,return=hash,include=inc);
%goto &amp;amp;action;

%compile:
%include &amp;amp;include;
%return;

%declare:
if _n_=1 then declare javaobj SASHash('SASHash');
%return;

%base64hmacsha256:
SASHash.callStringMethod("&amp;amp;action",strip(&amp;amp;key),strip(&amp;amp;message),&amp;amp;return);
%return;
%mend;

%let key = sdfoij3242039sdflkj3r23w345v;
%SASHash(compile,include=inc)


data in3.schdDent_step1;
	modify in3.schdent_step1;

	%SASHash(declare)
	length hashname $ 128;
	%SASHash(base64hmacsha256,key="&amp;amp;key",message=ssn,return=ssn);
    
    /*ssn = put(md5(upcase(trim(coalesce(ssn,''))) || '^' || '703447'),$hex32.)*/

    lastname = put(md5(upcase(trim(coalesce(lastname,''))) || '^' || '703447'),$hex32.);
    firstname = put(md5(upcase(trim(coalesce(firstname,''))) || '^' || '703447'),$hex32.);

run;

filename out1 '/new/dirct/loct/userLoc/dss20170615/schdDent.txt';

proc export data=in3.schdDent_step1
   outfile=out1
   dbms=dlm
   replace;
   delimiter='|';
run;


/*compresses a file with encryption by calling pkzip (available in AIX) */

data _null_;
    call system ('pkzipc -add -passphrase=$@asdfsdf!@@# /new/dirct/loct/userLoc/dss20170615/schdDent.zip /new/dirct/loct/userLoc/dss20170615/schdDent.txt');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The original code revised w/ yours incorporated is shown above. After changing the version we ended up adding your recommendation&amp;nbsp; verbatim right after the top Macro except the excerpt below we placed in the Data Step where we are hashing the ssn and return the result back to it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%SASHash(declare)
	length hashname $ 128;
	%SASHash(base64hmacsha256,key="&amp;amp;key",message=ssn,return=ssn);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The code runs and returns hashed ssn values. We'll wait for any further remarks/comments/advice from your end before we close the thread.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We thank you very much for your assistance and the time you put to develop an answer to our Q.&lt;/P&gt;&lt;P&gt;S-&lt;/P&gt;</description>
      <pubDate>Sat, 03 Feb 2018 07:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433768#M107571</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-03T07:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433932#M107649</link>
      <description>&lt;P&gt;Correction on my own last comment about the Data Step code that should be as follows in order to execute hashing the ssn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in3.schdDent_step1;

	%SASHash(declare)
	length hashname $ 128;
        set in3.schdent_step1;
	%SASHash(base64hmacsha256,key="&amp;amp;key",message=ssn,return=ssn);
    
    /*ssn = put(md5(upcase(trim(coalesce(ssn,''))) || '^' || '703447'),$hex32.)*/

    lastname = put(md5(upcase(trim(coalesce(lastname,''))) || '^' || '703447'),$hex32.);
    firstname = put(md5(upcase(trim(coalesce(firstname,''))) || '^' || '703447'),$hex32.);

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 04 Feb 2018 02:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/433932#M107649</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-04T02:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to apply HMAC-SHA256 hash to a column of a data set ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/436320#M108519</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dir1=%str(/new/dirct/loct/userLoc/logfiles/);
%let pgmname=%str(SchdDent_20170615);
%let date=&amp;amp;sysdate;
%let time=&amp;amp;systime;
%let ext1=%str(log);
%let break=%str(-);
%let final1=&amp;amp;dir1&amp;amp;pgmname&amp;amp;date&amp;amp;break&amp;amp;time..&amp;amp;ext1;
%put &amp;amp;final1;

%let key1 = %nrstr(kd94hf93k423kf44&amp;amp;pfkkdhi9sl3r4s00) ;

proc printto log="&amp;amp;final1";
run; quit;


* can be a physical file using;filename inc temp;
data _null_;
file inc;
input @;
put _infile_;
cards4;
filename cp temp;
proc groovy classpath=cp;

add sasjar="commons_codec" version="1.4.0.0_SAS_20100917120335"; *version is specific to SAS Installation and may differ from this;

submit parseonly;
 
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64

class SASHash { 
    String base64hmacsha256(String key, String message) {
        def mac = Mac.getInstance("HmacSHA256")
        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA256"))
        def hash = Base64.encodeBase64String(mac.doFinal(message.getBytes()))
        return hash
    }
}
 
endsubmit;
 
quit;

options set=classpath "%sysfunc(pathname(cp,f))";
;;;;
run;

* generic macro that can be expanded to include additional methods;
%macro SASHash(action,key=key,message=message,return=hash,include=inc);
%goto &amp;amp;action;

%compile:
%include &amp;amp;include;
%return;

%declare:
if _n_=1 then declare javaobj SASHash('SASHash');
%return;

%base64hmacsha256:
SASHash.callStringMethod("&amp;amp;action",strip(&amp;amp;key),strip(&amp;amp;message),&amp;amp;return);
%return;
%mend;

*key can be set per user's need;
%let key = sklnvhjsz34545456dfbzdaldmgd3;
%SASHash(compile,include=inc)


options nocenter ls=132 ps=32000 obs=50 compress=char;        

    libname in3 '/new/dirct/loct/userLoc/dss20170615/';
 
    
*Macro to read in one month of Sched Dent;        
%macro readSchdDent(fy);        
    libname inscen&amp;amp;fy. "/mainDb/publs/SchdDent/";    
	
	data SchdDent&amp;amp;fy.;	
		set inscen&amp;amp;fy..header; /*(keep = fldName)	*/
		/*if FM='01'  */
	run;

    proc append base = in3.SchdDent_step1 data = SchdDent&amp;amp;fy.  (compress=char);	
	run;    

	proc datasets;		
		delete SchdDent&amp;amp;fy.;
	run;

	data in3.SchdDent_step1;
                %SASHash(declare)
                length ssnhsh $ 128;

		set in3.SchdDent_step1 ;
                %SASHash(base64hmacsha256,key="&amp;amp;key",message=ssn,return=ssnhsh)
		&lt;BR /&gt;
		/* Add more flds to be encrypted by copying/pasting the code above and then replace the fld name */
		/* ssn = put(md5(upcase(trim(coalesce(ssn,''))) || '^' || '703617'),$hex32.); */
	run;

	filename out1 "/new/dirct/loct/userLoc/dss20170615/SchdDent_FY&amp;amp;fy..txt";

	proc export data=in3.SchdDent_step1 
	   outfile=out1 
	   dbms=dlm
	   replace;
	  /* putnames=yes; */
	   delimiter='|';
	run;

	data in3.SchdDent_step1;
		set in3.SchdDent_step1 (obs=0);
	run;

	%let glbFY = &amp;amp;fy.;

	data _null_ ;
		fyx = "&amp;amp;glbFY";
		command = 'pkzipc -add -passphrase=$@gR!xdgdfSAg /new/dirct/loct/userLoc/dss20170615/SchdDent_FY'||fyx||'.zip /new/dirct/loct/userLoc/dss20170615/SchdDent_FY'||fyx||'.txt';
		call system (command);
	run;


%mend readSchdDent;        

data _null_;
  do i=14 to 17 by 1;
	command1 ='%nrstr(%readSchdDent('||strip(i)||'))';
	call execute (command1);
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The code above incorporates FriedEgg's excerpt successfully and it returns 4 txt files and their zipped counterparts populated with an ssn hashed with HMAC SHA256 for 4 Fiscal Yrs&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sharing just in case someone else has a similar wonder.&lt;/P&gt;&lt;P&gt;S-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 18:58:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-apply-HMAC-SHA256-hash-to-a-column-of-a-data-set/m-p/436320#M108519</guid>
      <dc:creator>skalamat</dc:creator>
      <dc:date>2018-02-12T18:58:46Z</dc:date>
    </item>
  </channel>
</rss>

