Update models

This commit is contained in:
k.struzhkin 2019-05-24 18:01:39 +03:00
parent 201839e001
commit 465a70afe5
5 changed files with 90 additions and 1 deletions

View File

@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.rbkmoney</groupId>
<artifactId>adapter-bank-spring-boot-starter</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>

View File

@ -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

View File

@ -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);
}

View File

@ -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<String, String> 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<String, String> 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);
}
}
}

View File

@ -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());
}
}