-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the odds of a collision in image keys
Fixes #27
- Loading branch information
Sam Judd
committed
Oct 20, 2013
1 parent
775639a
commit 9600642
Showing
3 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
package com.bumptech.glide.util; | ||
|
||
public class Util { | ||
private static final int PRIME = 31; | ||
private static final char[] hexArray = "0123456789abcdef".toCharArray(); | ||
private static final char[] sha256Chars = new char[64]; //32 bytes from sha-256 -> 64 hex chars | ||
|
||
public static int hash(int... hashes) { | ||
int result = 1; | ||
for (int hash : hashes) { | ||
result *= PRIME * hash; | ||
public static String sha256BytesToHex(byte[] bytes) { | ||
return bytesToHex(bytes, sha256Chars); | ||
} | ||
|
||
// Taken from: | ||
// http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java/9655275#9655275 | ||
private static String bytesToHex(byte[] bytes, char[] hexChars) { | ||
int v; | ||
for ( int j = 0; j < bytes.length; j++ ) { | ||
v = bytes[j] & 0xFF; | ||
hexChars[j * 2] = hexArray[v >>> 4]; | ||
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | ||
} | ||
return result; | ||
return new String(hexChars); | ||
} | ||
} |