Skip to content

Commit 2063809

Browse files
author
Oleksii Zinchenko
committed
generateSign with utf-8 encoding
1 parent 8c96cb4 commit 2063809

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target
2+
classes
23
corezoid-sdk.iml
34
.idea

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.corezoid</groupId>
66
<artifactId>corezoid-sdk</artifactId>
7-
<version>2.1</version>
7+
<version>2.2</version>
88
<packaging>jar</packaging>
99

1010
<name>corezoid-sdk-${project.version}</name>

src/main/java/com/corezoid/sdk/entity/CorezoidMessage.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.corezoid.sdk.entity;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.security.*;
45
import java.util.*;
56
import javax.xml.bind.DatatypeConverter;
7+
68
import net.sf.json.*;
79

810
/**
@@ -72,10 +74,10 @@ public static CorezoidMessage request(String apiSecret, String apiLogin,
7274
/**
7375
* Check signature
7476
*
75-
* @param sign - @QueruParam SIGNATURE from url
77+
* @param sign - @QueruParam SIGNATURE from url
7678
* @param apiSecret - api secret key
77-
* @param time - @QueruParam GMT_UNIXTIME from url
78-
* @param content - request body
79+
* @param time - @QueruParam GMT_UNIXTIME from url
80+
* @param content - request body
7981
* @return true if signature is valid, or false
8082
*/
8183
public static boolean checkSign(String sign, String apiSecret, String time,
@@ -110,7 +112,7 @@ public static Map<String, String> parseAnswer(String jsonString) throws Exceptio
110112
return result;
111113
}
112114

113-
//----------------------------------------------------------------------------------------------------------------------
115+
//----------------------------------------------------------------------------------------------------------------------
114116
private CorezoidMessage(String body, String time, String apiSecret,
115117
String apiLogin) {
116118
this.body = body;
@@ -164,24 +166,32 @@ public int hashCode() {
164166
}
165167

166168
//----------------------------------------------------------------------------------------------------------------------
169+
167170
/**
168171
* Genarate signature {SIGNATURE} = hex( sha1({GMT_UNIXTIME} + {API_SECRET}
169172
* + {CONTENT} + {API_SECRET}) )
170173
*
171-
* @param time - time
174+
* @param time - time
172175
* @param apiSecret - apiSecret
173-
* @param body - request body
176+
* @param body - request body
174177
* @return - signature
175178
*/
176179
private static String generateSign(String time, String apiSecret,
177180
String body) {
178181
MessageDigest sha1 = messageDigest.get();
179182
sha1.reset();
180-
byte[] bytes = (time + apiSecret + body + apiSecret).getBytes();
181-
String sha1hex = DatatypeConverter.printHexBinary(sha1.digest(bytes)).toLowerCase();
183+
byte[] bytes;
184+
String sha1hex;
185+
try {
186+
bytes = (time + apiSecret + body + apiSecret).getBytes("UTF-8");
187+
sha1hex = DatatypeConverter.printHexBinary(sha1.digest(bytes)).toLowerCase();
188+
} catch (UnsupportedEncodingException e) {
189+
throw new RuntimeException("generateSign error", e);
190+
}
182191
return sha1hex;
183192
}
184-
//----------------------------------------------------------------------------------------------------------------------
193+
194+
//----------------------------------------------------------------------------------------------------------------------
185195
private static final ThreadLocal<MessageDigest> messageDigest = new ThreadLocal<MessageDigest>() {
186196
@Override
187197
protected MessageDigest initialValue() {
@@ -193,15 +203,16 @@ protected MessageDigest initialValue() {
193203
}
194204
};
195205

196-
//----------------------------------------------------------------------------------------------------------------------
206+
//----------------------------------------------------------------------------------------------------------------------
197207
private static JSONObject parseJson(Object content) throws Exception {
198208
try {
199209
return (JSONObject) JSONSerializer.toJSON(content);
200210
} catch (Exception ex) {
201211
throw new Exception(String.format("json parsing error. %s", ex.getMessage()));
202212
}
203213
}
204-
//----------------------------------------------------------------------------------------------------------------------
214+
215+
//----------------------------------------------------------------------------------------------------------------------
205216
public final String body;
206217
public final String url;
207218
private final String time;

0 commit comments

Comments
 (0)