11package com .corezoid .sdk .entity ;
22
3+ import java .io .UnsupportedEncodingException ;
34import java .security .*;
45import java .util .*;
56import javax .xml .bind .DatatypeConverter ;
7+
68import 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