Skip to content

Commit e6cc6c2

Browse files
Fix encoding issues when JVM default encoding is not UTF-8
1 parent cae822f commit e6cc6c2

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

cloudinary-core/src/main/java/com/cloudinary/Cloudinary.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public String apiSignRequest(Map<String, Object> paramsToSign, String apiSecret)
148148
} catch (NoSuchAlgorithmException e) {
149149
throw new RuntimeException("Unexpected exception", e);
150150
}
151-
byte[] digest = md.digest((to_sign + apiSecret).getBytes());
151+
byte[] digest = md.digest(getUTF8Bytes(to_sign + apiSecret));
152152
return StringUtils.encodeHexString(digest);
153153
}
154154

@@ -231,6 +231,14 @@ protected Map parseConfigUrl(String cloudinaryUrl) {
231231
return params;
232232
}
233233

234+
byte[] getUTF8Bytes(String string) {
235+
try {
236+
return string.getBytes("UTF-8");
237+
} catch (java.io.UnsupportedEncodingException e) {
238+
throw new RuntimeException("Unexpected exception", e);
239+
}
240+
}
241+
234242
@Deprecated
235243
public static Map asMap(Object... values) {
236244
return ObjectUtils.asMap(values);

cloudinary-core/src/main/java/com/cloudinary/Url.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public String generate(String source) {
360360

361361

362362

363-
byte[] digest = md.digest((toSign + this.config.apiSecret).getBytes());
363+
byte[] digest = md.digest(cloudinary.getUTF8Bytes(toSign + this.config.apiSecret));
364364
signature = Base64Coder.encodeURLSafeString(digest);
365365
signature = "s--" + signature.substring(0, 8) + "--" ;
366366
}
@@ -484,7 +484,7 @@ public String unsignedDownloadUrlPrefix(String source, String cloudName, boolean
484484

485485
private String shard(String input) {
486486
CRC32 crc32 = new CRC32();
487-
crc32.update(input.getBytes());
487+
crc32.update(cloudinary.getUTF8Bytes(input));
488488
return String.valueOf((crc32.getValue() % 5 + 5) % 5 + 1);
489489
}
490490

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void testCustomCoordinates() throws Exception {
303303
@Test
304304
public void testContext() throws Exception {
305305
//should allow sending context
306-
Map context = ObjectUtils.asMap("caption", "some caption", "alt", "alternative");
306+
Map context = ObjectUtils.asMap("caption", "some cäption", "alt", "alternativè");
307307
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("context", context));
308308
Map info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("context", true));
309309
assertEquals(ObjectUtils.asMap("custom", context), info.get("context"));

0 commit comments

Comments
 (0)