SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Java Code (Sha1.java):

import java.math.BigInteger;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class Sha1 {

    public static String text;

    public static void main(String[] args) throws NoSuchAlgorithmException {

        for (String item : args) {

            text = item;

            System.out.println("Text: " + item + " SHA1: " + getSha());

        }

    }

    public static String getSha() throws NoSuchAlgorithmException {

        byte[] digest = MessageDigest.getInstance("SHA1").digest(text.getBytes());

        return new BigInteger(1, digest).toString(16);

    }

}

Compile Class:

javac Sha1.java

Test Class:

java -cp . Sha1 abc abcd

Result of test:

Text: abc SHA1: a9993e364706816aba3e25717850c26c9cd0d89d

Text: abcd SHA1: 81fe8bfe87576c3ecb22426f8e57847382917acf

Launch SAS:

sas -SET CLASSPATH /path/to/class/folder

SAS Code;

data foo;

length text sha1 $40;

declare javaobj s('Sha1');

do text='abc','abcd';

  s.setStaticStringField('text',text);

  s.callStaticStringMethod('getSha',sha1);

  put 'Text: ' text ' SHA1: ' sha1;

end;

run;

SAS Output:

Text: abc SHA1: a9993e364706816aba3e25717850c26c9cd0d89d

Text: abcd SHA1: 81fe8bfe87576c3ecb22426f8e57847382917acf

Another SAS Example, SHA1 Hash all Names in sashelp.class:

data class;

if 0 then set sashelp.class;

length sha1 $40;

declare hash c(dataset:'sashelp.class',ordered:'y');

declare hiter i('c');

  c.definekey('sex','name');

  c.definedata(all:'y');

  c.definedone();

declare javaobj j('Sha1');

_n_=i.first();

do while(_n_=0);

  j.setStaticStringField('text',name);

  j.callStaticStringMethod('getSha',sha1);

  output;

  _n_=i.next();

end;

stop;

run;

This is somewhat a continuation of my previous postings on Ciphers and performing data masking in SAS:

Data masking.  Implement a cipher. http://communities.sas.com/message/106603

1 ACCEPTED SOLUTION
2 REPLIES 2
FriedEgg
SAS Employee

Here is an update to the above utilizing PROC GROOVY

filename cp temp;

proc groovy classpath=cp;

submit parseonly;

import java.security.MessageDigest;

class Sha1 {

   public String encode(String message) {

      return new BigInteger(1, MessageDigest.getInstance("SHA1").digest(message.getBytes())).toString(16);

   }

}

endsubmit;

quit;

options set=classpath "%sysfunc(pathname(cp,f))";

data class;

dcl javaobj sha1('Sha1');

do until (done);

   set sashelp.class end=done;

   length encoded_name $ 40;

   sha1.callStringMethod('encode', strip(name), encoded_name);

   output;

   end;

run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6715 views
  • 6 likes
  • 1 in conversation