Skip to content

Commit

Permalink
Merge pull request #11 from huy1912/feature/UUID-16-Length
Browse files Browse the repository at this point in the history
Fix the length issue generating by the least bits
  • Loading branch information
jaaufauvre authored Mar 7, 2019
2 parents 7008846 + cc214ec commit 8cd375b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/mastercard/developer/oauth/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -29,6 +28,8 @@ private OAuth() {

private static final Logger LOG = Logger.getLogger(OAuth.class.getName());
private static final String HASH_ALGORITHM = "SHA-256";
private static final int NONCE_LENGTH = 16;
private static final String ALPHA_NUMERIC_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

/**
* Creates a Mastercard API compliant OAuth Authorization header
Expand Down Expand Up @@ -171,13 +172,15 @@ static String toOauthParamString(SortedMap<String, List<String>> queryParamsMap,
* Generates a random string for replay protection as per
* https://tools.ietf.org/html/rfc5849#section-3.3
*
* @return concatenation of 3 characters from the most significant bits and 13 characters from the least significant bits without dashes.
* @return random string of 16 characters.
*/
static String getNonce() {
UUID uuid = UUID.randomUUID();
String least = Long.toString(uuid.getLeastSignificantBits(), Character.MAX_RADIX).replace("-", "");
String most = Long.toString(uuid.getMostSignificantBits(), Character.MAX_RADIX).replace("-", "").substring(0, 3);
return most + least;
SecureRandom rnd = new SecureRandom();
StringBuilder sb = new StringBuilder(NONCE_LENGTH);
for (int i = 0; i < NONCE_LENGTH; i++) {
sb.append(ALPHA_NUMERIC_CHARS.charAt(rnd.nextInt(ALPHA_NUMERIC_CHARS.length())));
}
return sb.toString();
}

/**
Expand Down

0 comments on commit 8cd375b

Please sign in to comment.