tag:blogger.com,1999:blog-27324814076556739582024-02-20T15:17:49.145-08:00carson63000-techCarson 63000http://www.blogger.com/profile/10900682924502279486noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2732481407655673958.post-89236201234804835922010-08-11T17:05:00.000-07:002010-08-11T17:56:10.024-07:00Don't use FormsAuthentication.HashPasswordForStoringInConfigFile()So, <a href="http://www.diaryofaninja.com/" target="_blank">a ninja of my acquaintance</a> told me yesterday that <a href="http://en.wikipedia.org/wiki/MD5" target="_blank">MD5</a> and <a href="http://en.wikipedia.org/wiki/SHA-1" target="_blank">SHA-1</a> hashes were not really considered acceptable for hashing passwords nowadays due to attacks having been developed which make it easier than it should be to find a hash collision. "Federal agencies should stop using SHA-1 for digital signatures, digital time stamping and other applications that require collision resistance as soon as practical," <a href="http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html" target="_blank">says the National Institute of Standards and Technology</a>.<br />
<br />
Now, many .NET developers are probably accustomed to using <tt>FormsAuthentication.HashPasswordForStoringInConfigFile()</tt> to hash passwords. So it's quite an annoyance that this function only supports two options for hash format: MD5 and SHA-1!<br />
<br />
Why is this so? I have no idea. The <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx" target="_blank"><tt>System.Security.Cryptography</tt></a> namespace contains implementations of the newer <a href="http://en.wikipedia.org/wiki/SHA-2" target="_blank">SHA-2</a> hash algorithms.<br />
<br />
Let's fire up <a href="http://www.red-gate.com/products/reflector/" target="_blank">.NET Reflector</a> and disassemble <tt>FormsAuthentication.HashPasswordForStoringInConfigFile()</tt><br />
<br />
<pre class="brush: csharp">public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}</pre><br />
That's some dodgy code. A hard-coded if-else-if construct to lock it down to only supporting MD5 and SHA-1, and they don't seem to be aware that there is actually an overload <a href="http://msdn.microsoft.com/en-us/library/wet69s13.aspx" target="_blank"><tt>HashAlgorithm.Create(string hashName)</tt></a> which takes a hash algorithm name parameter! If they'd just used that, they would have had support for all the SHA-2 hash implementations.<br />
<br />
Here's my alternative:<br />
<br />
<pre class="brush: csharp">public static string HashString(string inputString, string hashName)
{
HashAlgorithm algorithm = HashAlgorithm.Create(hashName);
if (algorithm == null)
{
throw new ArgumentException("Unrecognized hash name", "hashName");
}
byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
return Convert.ToBase64String(hash);
}</pre><br />
Handles all of the available hash formats (and indeed any new ones that might be added in later versions of .NET, if <tt>HashAlgorithm.Create(string hashName)</tt> is updated), and additionally returns the result Base64 encoded rather than as a hexadecimal number, so it's shorter for the same hash length.Carson 63000http://www.blogger.com/profile/10900682924502279486noreply@blogger.com9tag:blogger.com,1999:blog-2732481407655673958.post-55666378199285892432009-12-13T14:32:00.000-08:002009-12-13T14:52:20.213-08:00FCKeditor with ASP.NET. File uploads. Making them work in IE7 and Chrome.FCKeditor, now known as <a href="http://ckeditor.com/" target="_blank">CKEditor</a>, presumably because the original sounded too risqué, is quite a popular open-source rich text editor for use in web pages. I've used it in quite a few projects and always been pretty happy.<div><br /></div><div>Although the current release is CKEditor 3.0.1, ASP.NET developers are still using FCKeditor 2.6.5, since there isn't any ASP.NET integration in CKEditor 3 yet. And it was with 2.6.5 that I ran into a puzzling problem with file uploads.</div><div><br /></div><div>File uploading is disabled by default, but if you need to, you can enable it, and allow people editing text to upload files and link to the resulting upload. I'd only tried it once before, for a project that never went ahead and thus never left our test server, and it worked fine there.</div><div><br /></div><div>However, this time, once it was up on an external staging server.. the upload only worked with Firefox. With IE7 and Chrome, I'd hit the "Send it to the Server" button, and the progress indicator would spin forever, achieving nothing.</div><div><br /></div><div>So I dug a bit. Was the file being successfully uploaded? Yes, it was. Was an error being returned by the Ajax call? I inspected it with <a href="http://www.fiddler2.com/fiddler2/" target="_blank">Fiddler</a> and it looked OK - no error, a 200 response code sending back a chunk of Javascript to the browser. So obviously there was something wrong with the Javascript.</div><div><br /></div><div>Having narrowed it down to this bit of Javascript, and identified that it was being emitted by the function FileWorkerBase::SendFileUploadResponse() in the FCKeditor.NET library, I was able to Google with more confidence, and a solution was found at <a href="http://dev.fckeditor.net/ticket/2700" target="_blank">http://dev.fckeditor.net/ticket/2700</a></div><div><br /></div><div>Basically there is something going wrong with the Javascript's attempts to fiddle with document.domain. Removing is, as per that ticket, fixed the problem.<br /><ol><br /><li>Download the FCKeditor.NET library source from <a href="http://sourceforge.net/projects/fckeditor/files/FCKeditor.Net/2.6.3/FCKeditor.Net_2.6.3.zip/download" target="_blank">http://sourceforge.net/projects/fckeditor/files/FCKeditor.Net/2.6.3/FCKeditor.Net_2.6.3.zip/download</a></li><br /><li>Open up FileBrowser/FileWorkerBase.cs</li><br /><li>Replace the SendFileUploadResponse(int errorNumber, bool isQuickUpload, string fileUrl, string fileName, string customMsg) function on line 110 with the version from <a href="http://dev.fckeditor.net/ticket/2700" target="_blank">http://dev.fckeditor.net/ticket/2700</a></li><br /><li>Rebuild and replace your FredCK.FCKeditorV2.dll !</li><br /></ol><br /></div>Carson 63000http://www.blogger.com/profile/10900682924502279486noreply@blogger.com0tag:blogger.com,1999:blog-2732481407655673958.post-47126573769533086552009-11-26T20:39:00.000-08:002009-11-26T21:00:31.884-08:00Atlassian's Confluence. Tomcat. 64-bit Windows. Making it work as a service.So I was installing an evaluation copy of Atlassian's "<a target="_blank" href="http://www.atlassian.com/software/confluence/">Confluence</a>" wiki, and since we're mostly a Windows shop here, I was doing so on a Windows Server 2008 box.<div><br /></div><div>All went well until I got to step 9 of <a target="_blank" href="http://confluence.atlassian.com/display/DOC/Installing+Confluence+Standalone+on+Windows+from+Zip+File">the installation guide</a>: "Start Confluence automatically on Windows as a Service." I ran into the following problem when I got to the final "<span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size: small;">net start Confluence</span></span>" step.</div><div><br /></div><div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:08] [info] Procrun (2.0.5.0) started</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:08] [info] Running Service...</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:08] [info] Starting service...</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:08] [197 javajni.c] [error] %1 is not a valid Win32 application.</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:08] [994 prunsrv.c] [error] Failed creating java c:\Progra~1\java\jdk1.6.0_17\jre\bin\server\jvm.dll</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:09] [1269 prunsrv.c] [error] ServiceStart returned 1</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:09] [info] Run service finished.</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">[2009-11-27 14:51:09] [info] Procrun finished.</span></span></div><div><br /></div><div>As hinted in the "<a target="_blank" href="http://confluence.atlassian.com/display/DOC/Start+Confluence+automatically+on+Windows+as+a+Service">Start Confluence automatically on Windows as a Service</a>" document, there are issues getting Apache Tomcat to run as a service on 64-bit Windows using a 64-bit JDK. The recommended workaround is to use a 32-bit JDK, but since that's a bit lame, I decided to follow the advice of the issue "<a target="_blank" href="http://jira.atlassian.com/browse/CONF-12293">Installing Confluence as a service on XP-64 fails due to 32-bit binaries of Tomcat</a>" and try replacing the Tomcat binaries with 64-bit versions.</div><div><br /></div><div>And here's the bit that got me scratching my head for a while. Various things I googled up directed me to the Tomcat SVN repository to get the 64-bit version, but I'll be damned if I could find a version for x86-64 or x64. Just the old IA-64 Itanium stuff, and "AMD64", which I assumed was some obscure AMD implementation.</div><div><br /></div><div>More fool me. A trip to Wikipedia's <a target="_blank" href="http://en.wikipedia.org/wiki/X86-64">x86-64 article</a> clarified things: AMD64 was the marketing name AMD used for their x64-64 implementation, and those <i>were</i> the binaries I was looking for, for my Xeon server.</div><div><br /></div><div>If you need them, head on over to Tomcat's SVN repository at <span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"><a target="_blank" href="http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/res/procrun/amd64/">http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/res/procrun/amd64/</a></span></span> and grab tomcat.exe from there - it worked for me!</div><div><br /></div></div>Carson 63000http://www.blogger.com/profile/10900682924502279486noreply@blogger.com1