Coinbase { public static void sign(FIXMessage message, String secret) { StringBuilder presign = new StringBuilder(); appendField(message, SendingTime, "SendingTime(52)", presign); presign.append(SOH); presign.append(Logon); presign.append(SOH); appendField(message, MsgSeqNum, "MsgSeqNum(34)", presign); presign.append(SOH); appendField(message, SenderCompID, "SenderCompID(49)", presign); presign.append(SOH); appendField(message, TargetCompID, "TargetCompID(56)", presign); presign.append(SOH); appendField(message, Password, "Password(554)", presign); Key key = new SecretKeySpec(Base64.getDecoder().decode(secret), "HmacSHA256"); try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key); mac.update(presign.toString().getBytes(US_ASCII)); String sign = Base64.getEncoder().encodeToString(mac.doFinal()); message.addField(RawData).setString(sign); } catch (InvalidKeyException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } } private  Coinbase(); static void sign(FIXMessage message, String secret);  }
@Test public void sign() { FIXMessage message = new FIXMessage(64, 64); message.addField(SenderCompID).setString("foo"); message.addField(TargetCompID).setString("Coinbase"); message.addField(MsgSeqNum).setInt(1); message.addField(SendingTime).setString("20160118-09:30:00.000"); message.addField(EncryptMethod).setInt(EncryptMethodValues.None); message.addField(HeartBtInt).setInt(30); message.addField(Password).setString("bar"); String secret = "baz"; Coinbase.sign(message, secret); assertNotNull(message.valueOf(RawData)); }