I'm trying to create an AWS signature, however I can't get my Line Feeds ('0a'x) to work exactly right when I'm trying to output a textfile of the string I want to hash. I'm using a textfile to check that the string (to be stored as a macro variable) is correct and can be hashed correctly.
Due to the line feeds not being exact it's causing the hash to be different from what I'm expecting (even when hashing the string directly)
Here's what I have currently; from googling around various documents:
data _null_;
file "W:\sigtest.txt" TERMSTR=lf recfm=v;
signature = 'AWS4-HMAC-SHA256' || '0a'x ||
'20150915T124500Z' || '0a'x ||
'20150915/us-east-1/s3/aws4_request' || '0a'x ||
'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';
put signature;
run;
However when I check the output in notepad++ I get the following:
When I'm expecting the following:
The extra line causes the hash to be different, however if I drop:
TERMSTR=lf recfm=v
and use:
recfm=f
I get the requisite number of lines (4), but I have trailing blanks/spaces after the hash key, which I can't get rid of.
Appreciate any help anyone has for this problem.
You need RECFM=N if you want to write your own term-string. You also need a format you are getting a extra space using LIST PUT.
data _null_;
file "~/sigtest.txt" recfm=n;
signature = 'AWS4-HMAC-SHA256' || '0a'x ||
'20150915T124500Z' || '0a'x ||
'20150915/us-east-1/s3/aws4_request' || '0a'x ||
'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';
l=length(signature);
put signature $varying256. l;
run;
data _null_;
infile "~/sigtest.txt" recfm=f;
input;
list;
run;
My colleague suggested the following solution:
data _null_;
file "W:\sigtest.txt" TERMSTR=lf recfm=F lrecl=1;
signature = 'AWS4-HMAC-SHA256' || '0a'x ||
'20150915T124500Z' || '0a'x ||
'20150915/us-east-1/s3/aws4_request' || '0a'x ||
'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';
put signature;
run;
I will admit I'm not 100% sure how/why this is working but it seems to work. I'll take a look at your solution too data _null_.
RECFM N and F are similar in that there is no record termination string, RECFM=N has no record structure while RECFM=F has logical structure (an external attribute). Standing on the outside looking in both files look them same because they are (tested with unix diff)
Your example writes 133 one byte records. You can drop the INFILE statement option TERMSTR it is ignored.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.