diff options
| author | 2009-05-02 01:37:43 -0400 | |
|---|---|---|
| committer | 2009-05-02 01:37:43 -0400 | |
| commit | e90600f2885c322cce0dafa25b526c8fdf88a56f (patch) | |
| tree | 2b59eb4a46f438fafa5b10493680d944d0c5ba63 /source/util/Hasher.java | |
| download | ZPlayer-master.tar.xz ZPlayer-master.zip | |
Diffstat (limited to 'source/util/Hasher.java')
| -rw-r--r-- | source/util/Hasher.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/source/util/Hasher.java b/source/util/Hasher.java new file mode 100644 index 0000000..0458e94 --- /dev/null +++ b/source/util/Hasher.java @@ -0,0 +1,109 @@ +/* + * Created on Mar 1, 2004 + * Algorithm: + * Copyright (c) 2004 David Hammerton + * + * port to java: + * Copyright (c) 2004 Joseph Barnett + */ +package itunes.util; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * @author jbarnett + */ +public class Hasher { + //taken from calculations in libopendaap + //private static String staticHash = "98814088228B81E0AB021433618EC27B"; + private static String calcHash = ""; + private static boolean staticCalculated = false; + + private static final String hexchars = "0123456789ABCDEF"; + private static final String appleCopyright = "Copyright 2003 Apple Computer, Inc."; + private static MessageDigest md; + + private static final int HASH_SIZE = 32; + + static { + try { + md = MessageDigest.getInstance("md5"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + + private static String DigestToString(byte[] digest) + { + String string=""; + int i; + for (i = 0; i < digest.length; i++) + { + byte tmp = digest[i]; + string += hexchars.charAt((tmp >> 4) & 0x0f); + string += hexchars.charAt(tmp & 0x0f); + } + return string; + } + + private static synchronized void calculateStaticHash() { + if (staticCalculated) + return; + for (int i = 0; i < 256; i++) { + if ((i & 0x80) != 0) + md.update("Accept-Language".getBytes()); + else + md.update("user-agent".getBytes()); + + if ((i & 0x40) != 0) + md.update("max-age".getBytes()); + else + md.update("Authorization".getBytes()); + + if ((i & 0x20) != 0) + md.update("Client-DAAP-Version".getBytes()); + else + md.update("Accept-Encoding".getBytes()); + + if ((i & 0x10) != 0) + md.update("daap.protocolversion".getBytes()); + else + md.update("daap.songartist".getBytes()); + + if ((i & 0x08) != 0) + md.update("daap.songcomposer".getBytes()); + else + md.update("daap.songdatemodified".getBytes()); + + if ((i & 0x04) != 0) + md.update("daap.songdiscnumber".getBytes()); + else + md.update("daap.songdisabled".getBytes()); + + if ((i & 0x02) != 0) + md.update("playlist-item-spec".getBytes()); + else + md.update("revision-number".getBytes()); + + if ((i & 0x01) != 0) + md.update("session-id".getBytes()); + else + md.update("content-codes".getBytes()); + + String newHash =DigestToString(md.digest()); + calcHash += newHash; + } + staticCalculated = true; + } + + public static String GenerateHash(String url, int accessIndex) { + int start = HASH_SIZE * accessIndex; + int end = start + HASH_SIZE; + calculateStaticHash(); + md.update(url.getBytes()); + md.update(appleCopyright.getBytes()); + md.update(calcHash.substring(start, end).getBytes()); + return DigestToString(md.digest()); + } +} |
