public void testSHA1vsDecodedOwn() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { String hash = Base32.encode(CryptoTools.digest(TESTSTRINGS[i])); assertEquals( "TESTSTRINGS[" + i + "]", new String(CryptoTools.digest(TESTSTRINGS[i])), new String(Base32.decode(hash))); } }
public void testBase32Codec() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { // System.out.print("Encoding: "+TESTSTRINGS[i]+" ..."); final String encoded = Base32.encode(TESTSTRINGS[i]); System.out.println(" ->" + encoded); assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), Base32.decode(encoded)); } }
/** * Silly Microbenchmark * * @throws CryptoException */ public void testBenchmark() throws CryptoException { System.out.println("BigInteger encoding benchmarks:"); final int ITERATIONS = 100000; final Runtime runtime = Runtime.getRuntime(); long start = System.currentTimeMillis(); long memstart = runtime.freeMemory(); for (int i = 0; i < ITERATIONS; i++) { final String encoded = Base32.encode(TESTSTRINGS[i % TESTSTRINGS.length]); assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i % TESTSTRINGS.length], encoded); } long dur = System.currentTimeMillis() - start; long memuse = memstart - runtime.freeMemory(); System.out.println(ITERATIONS + " iterations took: " + dur + "ms"); System.out.println(ITERATIONS + " iterations used: " + memuse + " bytes"); /* System.out.println("\nWaterken encoding benchmarks:"); start=System.currentTimeMillis(); memstart=runtime.freeMemory(); for (int i=0;i<ITERATIONS;i++){ final String encoded = com.waterken.url.Base32.encode(TESTSTRINGS[i%TESTSTRINGS.length].getBytes()); assertEquals("TESTSTRINGS["+i+"]",TYLER_OUTPUT[i%TESTSTRINGS.length],encoded); } dur=System.currentTimeMillis()-start; memuse=memstart-runtime.freeMemory(); System.out.println(ITERATIONS+" iterations took: "+dur+"ms"); System.out.println(ITERATIONS+" iterations used: "+memuse+" bytes"); */ }
public void testDecodeTyler() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { final byte decoded[] = Base32.decode(TYLER_OUTPUT[i]); assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), decoded); } }
public void testBase32vsTyler() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { final String encoded = Base32.encode(TESTSTRINGS[i]); assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i], encoded); } }
public void testSHA1vsDecodedTyler() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { assertTrue( "TESTSTRINGS[" + i + "]", CryptoTools.equalByteArrays( CryptoTools.digest(TESTSTRINGS[i]), Base32.decode(TYLER_SHA1_OUTPUT[i]))); } }
public void testSHA1HomevsTyler() throws CryptoException { for (int i = 0; i < TESTSTRINGS.length; i++) { assertEquals( "TESTSTRINGS[" + i + "]", TYLER_SHA1_OUTPUT[i], Base32.encode(CryptoTools.digest(TESTSTRINGS[i]))); } }
public void testLength() throws CryptoException { assertEquals(32, Base32.encode(CryptoTools.digest("hello")).length()); assertEquals(8, Base32.encode("hello").length()); assertEquals(10, Base32.encode("hello1").length()); assertEquals(12, Base32.encode("hello12").length()); assertEquals(13, Base32.encode("hello123").length()); assertEquals(15, Base32.encode("hello1234").length()); assertEquals(16, Base32.encode("hello12345").length()); assertEquals(18, Base32.encode("hello123456").length()); }
public String legalizeUsername(String friendlyUsername) { try { if (!ILLEGAL_USERNAME_PATTERN.matcher(friendlyUsername).find()) return friendlyUsername; MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(WeaveUtil.toAsciiBytes(friendlyUsername.toLowerCase())); byte[] baseEncodedBytes = Base32.encode(digest.digest()); return WeaveUtil.toAsciiString(baseEncodedBytes); } catch (GeneralSecurityException e) { throw new Error(e); } }
private static void updateMetaData_handleReply( TOTorrent torrent, String hash, String replyType, Map mapHashes) { if (hash == null && torrent != null) { try { hash = torrent.getHashWrapper().toBase32String(); } catch (Exception e) { } } GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager(); DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash))); if (torrent == null && dm != null) { torrent = dm.getTorrent(); } Map contentMap = PlatformTorrentUtils.getContentMap(torrent); final TOTorrent torrentFinal = torrent; if (replyType.equals(PlatformMessenger.REPLY_EXCEPTION)) { if (torrent != null) { // try again in a bit log(torrent, "Exception, retrying later"); SimpleTimer.addEvent( "Update MD Retry", SystemTime.getCurrentTime() + RETRY_METADATA, new TimerEventPerformer() { public void perform(TimerEvent event) { log(torrentFinal, "retry time"); PlatformTorrentUtils.updateMetaData(torrentFinal, 15000); } }); } } else { Map jsonMapMetaData = hash == null ? null : (Map) mapHashes.get(hash); if (jsonMapMetaData != null) { long oldLastUpdated = getContentLastUpdated(torrent); long expireyMins = 0; for (Iterator iter = jsonMapMetaData.keySet().iterator(); iter.hasNext(); ) { String key = (String) iter.next(); Object value = jsonMapMetaData.get(key); if (value == null || value.equals(null)) { contentMap.remove(key); } else if ((key.equals("Thumbnail") || key.endsWith(".B64")) && value instanceof String) { contentMap.put(key, Base64.decode((String) value)); } else if (key.equals("expires-in-mins") && value instanceof Long) { expireyMins = ((Long) value).longValue(); } else { contentMap.put(key, value); } writeTorrentIfExists(torrent); } // crappy way of updating the display name if (dm != null) { String title = PlatformTorrentUtils.getContentTitle(torrent); if (title != null && title.length() > 0 && dm.getDownloadState().getDisplayName() == null) { dm.getDownloadState().setDisplayName(title); } } triggerMetaDataUpdateListeners(torrent); if (torrent != null) { // setup next refresh long refreshOn; if (expireyMins > 0) { refreshOn = SystemTime.getCurrentTime() + (expireyMins * 60 * 1000L); } else { long newLastUpdated = getContentLastUpdated(torrent); long diff = newLastUpdated - oldLastUpdated; log( torrent, "Last Updated: new " + new Date(newLastUpdated) + ";old " + new Date(oldLastUpdated) + ";diff=" + diff); if (diff > 0 && oldLastUpdated != 0) { diff *= 2; if (diff < MIN_MD_REFRESH_MS) { diff = MIN_MD_REFRESH_MS; } else if (diff > MAX_MD_REFRESH_MS) { diff = MAX_MD_REFRESH_MS; } refreshOn = SystemTime.getOffsetTime(diff); } else { refreshOn = SystemTime.getCurrentTime() + (7 * 24 * 60 * 60 * 1000L); } } log(torrent, "got MD. Next refresh in " + (refreshOn - SystemTime.getCurrentTime())); setMetaDataRefreshOn(torrent, refreshOn); SimpleTimer.addEvent( "Update MD", refreshOn, new TimerEventPerformer() { public void perform(TimerEvent event) { PlatformTorrentUtils.updateMetaData(torrentFinal, 15000); } }); } } else if (torrent != null) { long refreshOn = SystemTime.getCurrentTime() + (30 * 24 * 60 * 60 * 1000L); setMetaDataRefreshOn(torrent, refreshOn); log(torrent, "no hash in reply. Next refresh on " + new Date(refreshOn)); } } }