diff --git a/pom.xml b/pom.xml index 1b6641d..075d04f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.rbkmoney adapter-bank-spring-boot-starter - 0.0.3-SNAPSHOT + 0.0.4-SNAPSHOT jar diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/OperationModel.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/OperationModel.java index eef7da3..204d5f1 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/OperationModel.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/model/OperationModel.java @@ -1,6 +1,7 @@ package com.rbkmoney.adapter.bank.spring.boot.starter.model; import com.rbkmoney.damsel.cds.ExpDate; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.ToString; @@ -9,6 +10,7 @@ import java.util.Map; @Data @Builder +@AllArgsConstructor public class OperationModel { @ToString.Exclude diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java index 9fd4d2c..9232507 100644 --- a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/CardDateConverter.java @@ -13,10 +13,18 @@ public class CardDateConverter { return String.format("%1$02d", expDate.getYear() % 100); } + public static String getYearFromShort(short year) { + return String.format("%1$02d", year % 100); + } + public static String getMonthFromExpDate(ExpDate expDate) { return String.format("%02d", expDate.getMonth()); } + public static String getMonthFromByte(byte month) { + return String.format("%02d", month); + } + public static BigDecimal getFormattedAmount(long amount) { return new BigDecimal(amount).movePointLeft(2); } diff --git a/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java new file mode 100644 index 0000000..9199bb3 --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGenerator.java @@ -0,0 +1,60 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.Hex; +import org.springframework.util.MultiValueMap; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class SignGenerator { + + public static String prepareDataForHmac(String[] fields, MultiValueMap params) { + StringBuilder dataHmac = new StringBuilder(); + Arrays.asList(fields) + .forEach(field -> { + if (params.get(field) != null + && !params.get(field).isEmpty() + && params.get(field).get(0) != null + && !params.get(field).get(0).isEmpty() + ) { + dataHmac.append(params.get(field).get(0).length()); + dataHmac.append(params.get(field).get(0)); + } else { + dataHmac.append("-"); + } + } + ); + + return dataHmac.toString(); + } + + public static String sign(String[] fieldsForSign, MultiValueMap params, String key, String algorithm) { + String dataHmac = prepareDataForHmac(fieldsForSign, params); + String pSign = sign(dataHmac, key, algorithm); + return pSign.toUpperCase(); + } + + public static String sign(String data, String key, String algorithm) { + try { + byte[] decodedKey = Hex.decodeHex(key.toCharArray()); + SecretKeySpec keySpec = new SecretKeySpec(decodedKey, algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(keySpec); + + byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); + byte[] signatureBytes = mac.doFinal(dataBytes); + + return new String(new Hex().encode(signatureBytes)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } +} diff --git a/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java new file mode 100644 index 0000000..9df5f66 --- /dev/null +++ b/src/test/java/com/rbkmoney/adapter/bank/spring/boot/starter/utils/SignGeneratorTest.java @@ -0,0 +1,19 @@ +package com.rbkmoney.adapter.bank.spring.boot.starter.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.security.NoSuchAlgorithmException; + +public class SignGeneratorTest { + + public static final String HMAC_SHA_1 = "HmacSHA1"; + + @Test + public void sign() throws NoSuchAlgorithmException { + String test = "511.483USD677144616IT Books. Qty: 217Books Online Inc.14www.sample.com1512345678901234589999999919pgw@mail.sample.com11--142003010515302116F2B2DD7E603A7ADA33https://www.sample.com/shop/reply"; + + String s = SignGenerator.sign(test, "00112233445566778899AABBCCDDEEFF", HMAC_SHA_1); + Assert.assertEquals("FACC882CA67E109E409E3974DDEDA8AAB13A5E48", s.toUpperCase()); + } +} \ No newline at end of file