<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: How to make MD5, Java and Unicode play nicely</title>
	<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely/</link>
	<description>Comments on Ask MetaFilter post How to make MD5, Java and Unicode play nicely</description>
	<pubDate>Mon, 10 Dec 2007 20:01:29 -0800</pubDate>
	<lastBuildDate>Mon, 10 Dec 2007 20:01:29 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: How to make MD5, Java and Unicode play nicely</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely</link>	
		<description>How do I make MD5, Java and Unicode play nicely together? &lt;br /&gt;&lt;br /&gt; I don&apos;t really get unicode and character sets.  Now I need to. I&apos;m trying to reproduce the same effect in java that &lt;a href=&quot;http://pajhome.org.uk/crypt/md5/md5src.html&quot;&gt;this javascript&lt;/a&gt; gets when the &lt;code&gt;chrsz&lt;/code&gt; variable is set to 16.  When the variable is set to 8, the &lt;a href=&quot;http://pajhome.org.uk/crypt/md5/contrib/md5.java.txt&quot;&gt;java version&lt;/a&gt; gives the same result as the javascript.  I need the java version to give the same result as the javascript version does when &lt;code&gt;chrsz&lt;/code&gt; equals 16.&lt;br&gt;
&lt;br&gt;
I&apos;m not great at java.  I know enough to be dangerous.  I thought I was pretty hot stuff at javascript, but it might help if I knew what &amp;lt;&amp;lt; or &amp;gt;&amp;gt;&amp;gt; meant in javascript.  As you can imagine, that&apos;s really hard to google.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2007:site.78326</guid>
		<pubDate>Mon, 10 Dec 2007 19:44:55 -0800</pubDate>
		<dc:creator>idb</dc:creator>
		
			<category>javascript</category>
		
			<category>md5</category>
		
			<category>java</category>
		
			<category>unicode</category>
		
			<category>endian</category>
		
			<category>resolved</category>
		
			<category>bitwise</category>
		
	</item> <item>
		<title>By: sanko</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163133</link>	
		<description>I can&apos;t help with the MD5 problem, but if you&apos;re looking for the meaning of &amp;lt;&amp;lt; or &amp;gt;&amp;gt;&amp;gt; the search you want is &quot;shift operator.&quot;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163133</guid>
		<pubDate>Mon, 10 Dec 2007 20:01:29 -0800</pubDate>
		<dc:creator>sanko</dc:creator>
	</item><item>
		<title>By: kdar</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163136</link>	
		<description>Those &amp;lt;&amp;lt;, &amp;gt;&amp;gt;, and &amp;gt;&amp;gt;&amp;gt; are &lt;a href=&quot;http://www.javascriptkit.com/jsref/operators.shtml&quot;&gt;bitwise shift operators&lt;/a&gt;.  They appear to be &lt;a href=&quot;http://www.leepoint.net/notes-java/data/expressions/bitops.html&quot;&gt;the same in java&lt;/a&gt;.  (Sorry for not giving you a more in-depth explanation, but maybe this will illuminate things a bit for you.)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163136</guid>
		<pubDate>Mon, 10 Dec 2007 20:02:20 -0800</pubDate>
		<dc:creator>kdar</dc:creator>
	</item><item>
		<title>By: burnmp3s</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163148</link>	
		<description>Why are you trying to re-implement MD5 in Java?  It&apos;s already &lt;a href=&quot;http://snippets.dzone.com/posts/show/3686&quot;&gt;built into the standard libraries&lt;/a&gt;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163148</guid>
		<pubDate>Mon, 10 Dec 2007 20:22:34 -0800</pubDate>
		<dc:creator>burnmp3s</dc:creator>
	</item><item>
		<title>By: sbutler</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163291</link>	
		<description>I agree with burnmp3s... except I wouldn&apos;t use the String.getBytes() method. You don&apos;t want to trust that the platform encoding of a string is the same as the one your JS methods are using.&lt;br&gt;
&lt;br&gt;
Now, I don&apos;t really understand the MD5 algo, but I do know Unicode. According to your JS source, it is encoding the unicode strings as UTF-16LE (16 bit code points, little endian byte order). &lt;em&gt;If that&apos;s what the code actually does&lt;/em&gt; (and I&apos;m too tired to verify it) then this Java bit should work:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;MessageDigest m = MessageDigest.getInstance( &quot;MD5&quot; );&lt;br&gt;m.update( aString.getBytes( &quot;UTF-16LE&quot; ), 0, aString.length() );&lt;br&gt;System.out.println( &quot;MD5: &quot; + new BigInteger( 1, m.digest() ).toString( 16 ) );&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
If that DOESN&apos;T work, then your JS version is broken. Find a better one.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163291</guid>
		<pubDate>Tue, 11 Dec 2007 00:12:47 -0800</pubDate>
		<dc:creator>sbutler</dc:creator>
	</item><item>
		<title>By: idb</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163347</link>	
		<description>Thank you all for the responses.  The first thing I tried was to implement the standard libraries.  They do fine with the 8-bit setting and give exactly the same result.  I just couldn&apos;t get them to give the same result as when the variable was set to 16-bit.&lt;br&gt;
&lt;br&gt;
Going to try again based on sbutler&apos;s code.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163347</guid>
		<pubDate>Tue, 11 Dec 2007 04:06:08 -0800</pubDate>
		<dc:creator>idb</dc:creator>
	</item><item>
		<title>By: sbutler</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163479</link>	
		<description>Ohhh... wait a minute. That code above is broken (I copied it from the website burnmp3 listed). aString.getBytes( &quot;UTF-16LE&quot; ).length() != aString.length(). Try this instead:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;MessageDigest m = MessageDigest.getInstance( &quot;MD5&quot; );&lt;br&gt;byte aStringBytes[] = aString.getBytes( &quot;UTF-16LE&quot; );&lt;br&gt;m.update( aStringBytes, 0, aStringBytes.length() );&lt;br&gt;System.out.println( &quot;MD5: &quot; + new BigInteger( 1, m.digest() ).toString( 16 ) );&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163479</guid>
		<pubDate>Tue, 11 Dec 2007 07:43:19 -0800</pubDate>
		<dc:creator>sbutler</dc:creator>
	</item><item>
		<title>By: idb</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163732</link>	
		<description>sbutler, you are awesome!&lt;br&gt;
&lt;br&gt;
For reference sake, what worked was &lt;code&gt;aStringBytes.length&lt;/code&gt; without parentheses.  Also, there appears to be a leading zero that I have to worry about.  That aside, it works.&lt;br&gt;
&lt;br&gt;
Final proof of concept code - MD5.java:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;&lt;br&gt;
    import java.security.*;&lt;br&gt;
    import java.math.*;&lt;br&gt;
&lt;br&gt;
    public class MD5 {&lt;br&gt;
       public static void main(String args[]) throws Exception{&lt;br&gt;
            String aString = &quot;teststring&quot;;&lt;br&gt;
            MessageDigest m = MessageDigest.getInstance(&quot;MD5&quot;);&lt;br&gt;
            byte aStringBytes[] = aString.getBytes( &quot;UTF-16LE&quot; );&lt;br&gt;
            m.update( aStringBytes, 0, aStringBytes.length );&lt;br&gt;
            System.out.println( &quot;MD5: &quot; + new BigInteger( 1, m.digest() ).toString( 16 ) );&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
Thank you.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163732</guid>
		<pubDate>Tue, 11 Dec 2007 10:41:07 -0800</pubDate>
		<dc:creator>idb</dc:creator>
	</item><item>
		<title>By: idb</title>
		<link>http://ask.metafilter.com/78326/How-to-make-MD5-Java-and-Unicode-play-nicely#1163737</link>	
		<description>Thank you also to kdar and sanko for the explanations of bitwise operators.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.78326-1163737</guid>
		<pubDate>Tue, 11 Dec 2007 10:45:40 -0800</pubDate>
		<dc:creator>idb</dc:creator>
	</item>
	</channel>
</rss>
