Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3d8373

Browse files
removed apache commons dependencies since there is a way to base64-encode an array of bytes in the JDK (though it's hidden)
1 parent 045914d commit b3d8373

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
</scm>
4545

4646
<dependencies>
47-
<dependency>
48-
<groupId>commons-codec</groupId>
49-
<artifactId>commons-codec</artifactId>
50-
<version>1.4</version>
51-
</dependency>
5247

5348
<dependency>
5449
<groupId>junit</groupId>

src/main/java/org/scribe/services/HMACSha1SignatureService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import javax.crypto.*;
44
import javax.crypto.spec.*;
55

6-
import org.apache.commons.codec.binary.*;
76
import org.scribe.exceptions.*;
87
import org.scribe.utils.*;
98

9+
// implementation of base64 encoding lives here.
10+
import javax.xml.bind.DatatypeConverter;
11+
1012
/**
1113
* HMAC-SHA1 implementation of {@SignatureService}
1214
*
@@ -44,7 +46,12 @@ private String doSign(String toSign, String keyString) throws Exception
4446
Mac mac = Mac.getInstance(HMAC_SHA1);
4547
mac.init(key);
4648
byte[] bytes = mac.doFinal(toSign.getBytes(UTF8));
47-
return new String(Base64.encodeBase64(bytes), UTF8).replace(CARRIAGE_RETURN, EMPTY_STRING);
49+
return bytesToBase64String(bytes).replace(CARRIAGE_RETURN, EMPTY_STRING);
50+
}
51+
52+
private String bytesToBase64String(byte[] bytes)
53+
{
54+
return DatatypeConverter.printBase64Binary(bytes);
4855
}
4956

5057
/**

src/main/java/org/scribe/services/RSASha1SignatureService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.scribe.services;
22

3-
import org.apache.commons.codec.binary.*;
43
import org.scribe.exceptions.*;
54
import java.security.*;
65

6+
// implementation of base64 encoding lives here.
7+
import javax.xml.bind.DatatypeConverter;
8+
79
/**
810
* A signature service that uses the RSA-SHA1 algorithm.
911
*/
@@ -30,14 +32,19 @@ public String getSignature(String baseString, String apiSecret, String tokenSecr
3032
Signature signature = Signature.getInstance(RSA_SHA1);
3133
signature.initSign(privateKey);
3234
signature.update(baseString.getBytes(UTF8));
33-
return new String(Base64.encodeBase64(signature.sign(), false), UTF8);
35+
return bytesToBase64String(signature);
3436
}
3537
catch (Exception e)
3638
{
3739
throw new OAuthSignatureException(baseString, e);
3840
}
3941
}
4042

43+
private String bytesToBase64String(Signature signature) throws SignatureException
44+
{
45+
return DatatypeConverter.printBase64Binary(signature.sign());
46+
}
47+
4148
/**
4249
* {@inheritDoc}
4350
*/

src/test/java/org/scribe/services/RSASha1SignatureServiceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import static org.junit.Assert.*;
44

55
import org.junit.*;
6-
import org.apache.commons.codec.binary.*;
6+
7+
import javax.xml.bind.DatatypeConverter;
78
import java.security.*;
89
import java.security.spec.*;
910

@@ -55,7 +56,7 @@ private static PrivateKey getPrivateKey()
5556
try
5657
{
5758
KeyFactory fac = KeyFactory.getInstance("RSA");
58-
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(str.getBytes()));
59+
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(DatatypeConverter.parseBase64Binary(str));
5960
return fac.generatePrivate(privKeySpec);
6061
}
6162
catch (Exception e)

0 commit comments

Comments
 (0)