Skip to content

Commit 39d5dac

Browse files
authored
Merge pull request #93 from rembjo0/topic-encoding
Adds UTF-8 encoding in Util base64 encode/decode
2 parents a183afd + bbe081b commit 39d5dac

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

  • core/src/main/java/com/onelogin/saml2/util

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,9 @@ public static String convertDocumentToString(Document doc, Boolean c14n) {
330330
XMLUtils.outputDOM(doc, baos);
331331
}
332332

333-
return Util.toUtf8String(baos.toByteArray());
333+
return Util.toStringUtf8(baos.toByteArray());
334334
}
335-
336-
private static String toUtf8String(byte[] bytes) {
337-
try {
338-
return new String(bytes, "UTF-8");
339-
} catch (UnsupportedEncodingException e) {
340-
throw new IllegalStateException(e);
341-
}
342-
}
343-
335+
344336
/**
345337
* Converts an XML in Document format in a String without applying the c14n transformation
346338
*
@@ -655,7 +647,7 @@ public static String deflatedBase64encoded(String input) throws IOException {
655647
* @return the base64 encoded string
656648
*/
657649
public static String base64encoder(byte [] input) {
658-
return new String(Base64.encodeBase64(input));
650+
return toStringUtf8(Base64.encodeBase64(input));
659651
}
660652

661653
/**
@@ -667,7 +659,7 @@ public static String base64encoder(byte [] input) {
667659
* @return the base64 encoded string
668660
*/
669661
public static String base64encoder(String input) {
670-
return base64encoder(input.getBytes());
662+
return base64encoder(toBytesUtf8(input));
671663
}
672664

673665
/**
@@ -691,7 +683,7 @@ public static byte[] base64decoder(byte [] input) {
691683
* @return the base64 decoded bytes
692684
*/
693685
public static byte[] base64decoder(String input) {
694-
return base64decoder(input.getBytes());
686+
return base64decoder(toBytesUtf8(input));
695687
}
696688

697689
/**
@@ -1395,4 +1387,21 @@ public static DateTime parseDateTime(String dateTime) {
13951387
return parsedData;
13961388
}
13971389

1390+
private static String toStringUtf8(byte[] bytes) {
1391+
try {
1392+
return new String(bytes, "UTF-8");
1393+
} catch (UnsupportedEncodingException e) {
1394+
throw new IllegalStateException(e);
1395+
}
1396+
}
1397+
1398+
private static byte[] toBytesUtf8(String str) {
1399+
try {
1400+
return str.getBytes("UTF-8");
1401+
} catch (UnsupportedEncodingException e) {
1402+
throw new IllegalStateException(e);
1403+
}
1404+
}
1405+
1406+
13981407
}

0 commit comments

Comments
 (0)