<isofield
   id="64"
   length="64"
   name="MESSAGE AUTHENTICATION CODE FIELD"
   class="org.jpos.iso.IF_CHAR"/>
 <isofield
We decided to customise and change the length as the MAC being generated is of let 64 and it is Hex value.Â
This is the method that is setting that is used to set the MAC the getB4packager references the iso87Ascii.xml above :
   public ISOMsg setMAC(ISOMsg b24msg,String sessionkey,BigFootUtility bigfootUtil) throws ISOException
  {
    int yy=0;
   int maxfield=bigfootUtil.getLargestInt(bigfootUtil.ISOFields(b24msg));
    //logger.info("largetst field: "+maxfield);
    ISOMsg newIsoMessage = b24msg;
    try
    {
     newIsoMessage.setPackager(bigfootUtil.getB4Packager());
     //String dummyMAC=tlib.bytesToHex(new byte[32]);
      String dummyMAC=DatatypeConverter.printHexBinary(new byte[32]);
//Â Â Â Â Â Â logger.info("dummyMAC: "+dummyMAC);
    byte[] isoBytes=newIsoMessage.pack();      Â
      String isoString=new String(isoBytes);Â
       logger.info("Iso Message After MACing:"+ ISOUtil.dumpString(newIsoMessage.pack()));
      String mac=new TripDES().MessageMAC(sessionkey,isoBytes);
      logger.info("MAC: "+mac);
      if(maxfield>64)
      {
      newIsoMessage.set(128, mac);
        //logger.info("new mac 128: "+mac);
      }else
      {
        newIsoMessage.set(64, mac);
        //logger.info("new mac 64: "+mac);
      }
    }
    catch(Exception ex)
    {
      logger.error("error generaing MAC for msg: "+ex.getMessage()+"\n"+Arrays.toString(ex.getStackTrace()));
    }    Â
     logger.info("Iso Message After Packing and macing :"+ ISOUtil.dumpString(newIsoMessage.pack()));
  new NetworkUtils().ShowISOMessage(newIsoMessage, "Looping through ISO");
     return newIsoMessage;
  }
This is the inner MessageMac() Method that returns the Hash text :
public String MessageMAC(String clearSessionKey, byte[] macDataBytes) throws Exception
    {
      byte [] keyBytes = h2b(clearSessionKey);
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      digest.update(keyBytes, 0, keyBytes.length);
      digest.update(macDataBytes, 0, macDataBytes.length);
      byte[] hashedBytes = digest.digest();
      String hashText = b2h(hashedBytes);
      hashText = hashText.replace(" ", "");
      if (hashText.length() < 64)
      {
        int numberOfZeroes = 64 - hashText.length();
        String zeroes = "";
        String temp = hashText.toString();
        for (int i = 0; i < numberOfZeroes; i++)
          zeroes = zeroes + "0";
        temp = zeroes + temp;
        return temp;
      }
      return hashText;
    }
This is the. ValidateMAC() method being used by the client to check if Mac match :
boolean validateMAC(ISOMsg b24msg,String isob24msgstring,String clear_session_key,BigFootUtility bigfootUtil)
  {
 ISOMsg newIsoMsg=b24msg;Â
    String originalMAC="";String mac="";originalMAC=""; boolean isMacValidated=false;
    try
    {
      int maxfield=bigfootUtil.getLargestInt(bigfootUtil.ISOFields(b24msg));         Â
     if(maxfield>64)
      {
        originalMAC=newIsoMsg.getString(128);
      }
      else
      {
        originalMAC=newIsoMsg.getString(64);
      }
  String ismsg=isob24msgstring.substring(0, isob24msgstring.length()-64);
      //logger.info("isostring: "+ismsg);
      mac=new TripDES().MessageMAC(clear_session_key,ismsg.getBytes());
      logger.info("msg mac: "+mac);
      if(mac.toLowerCase().equals(originalMAC.toLowerCase())){isMacValidated=true;}      Â
    }
    catch(Exception ex)
    {
      logger.error("error validating mac: "+ex.getMessage()+"\n"+Arrays.toString(ex.getStackTrace()));
    }    Â
    return isMacValidated;
  }
Will really appreciate help and where I am doing things wrong .
Thanks
--
--
jPOS is licensed under AGPL - free for community usage for your open-source project. Licenses are also available for commercial usage. Please support jPOS, contact: sa...@jpos.org
---
You received this message because you are subscribed to the Google Groups "jPOS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jpos-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/41350260-73df-4db7-a6c6-2cf34848a220n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/b37fd0f4-eba7-4796-9396-62cc1367aba6n%40googlegroups.com.