Skip to content

Commit 01aa763

Browse files
Added encoding class
1 parent d7c7ea1 commit 01aa763

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ and the dependency:
2323
<dependency>
2424
<groupId>com.mauriciotogneri</groupId>
2525
<artifactId>javautils</artifactId>
26-
<version>1.5.0</version>
26+
<version>1.7.0</version>
2727
</dependency>
2828
```
2929

@@ -32,6 +32,6 @@ or if you use Gradle:
3232
```groovy
3333
dependencies
3434
{
35-
compile 'com.mauriciotogneri:javautils:1.5.0'
35+
compile 'com.mauriciotogneri:javautils:1.7.0'
3636
}
3737
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<modelVersion>4.0.0</modelVersion>
88
<groupId>com.mauriciotogneri</groupId>
99
<artifactId>javautils</artifactId>
10-
<version>1.5.0</version>
10+
<version>1.7.0</version>
1111
<name>Java Utils</name>
1212
<packaging>jar</packaging>
1313
<url>https://github.com/mauriciotogneri/java-utils</url>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.mauriciotogneri.javautils;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLDecoder;
5+
import java.net.URLEncoder;
6+
import java.security.MessageDigest;
7+
import java.security.NoSuchAlgorithmException;
8+
9+
public class Encoding
10+
{
11+
private static final String UTF8 = "UTF-8";
12+
13+
public static String toString(byte[] bytes) throws UnsupportedEncodingException
14+
{
15+
return new String(bytes, UTF8);
16+
}
17+
18+
public static byte[] toByteArray(String text) throws UnsupportedEncodingException
19+
{
20+
return text.getBytes(UTF8);
21+
}
22+
23+
public static String urlEncode(String text) throws Exception
24+
{
25+
return URLEncoder.encode(text, UTF8);
26+
}
27+
28+
public static String urlDecode(String text) throws Exception
29+
{
30+
return URLDecoder.decode(text, UTF8);
31+
}
32+
33+
public static String sha256(String input) throws NoSuchAlgorithmException
34+
{
35+
return hash("SHA-256", input);
36+
}
37+
38+
public static String sha512(String input) throws NoSuchAlgorithmException
39+
{
40+
return hash("SHA-512", input);
41+
}
42+
43+
public static String hash(String algorithm, String input) throws NoSuchAlgorithmException
44+
{
45+
MessageDigest digest = MessageDigest.getInstance(algorithm);
46+
digest.update(input.getBytes());
47+
byte messageDigest[] = digest.digest();
48+
49+
StringBuilder hexString = new StringBuilder(messageDigest.length * 2);
50+
51+
for (byte element : messageDigest)
52+
{
53+
hexString.append(String.format("%02x", element));
54+
}
55+
56+
return hexString.toString();
57+
}
58+
}

0 commit comments

Comments
 (0)