hash.xsh

Calculate SHA1 or MD5 hash over a set of files

<?xml version="1.0" encoding="utf-8"?>
<xsharper ID="HASH" xmlns="http://www.xsharper.com/schemas/1.0">
  <versionInfo value="Calculate SHA1 or MD5 over a set of files. Each file is calculated individually." />
  <usage options="ifHelp ifNoArguments default" lineSuffix=" [switches]" />
  <param count="multiple" value="Files to hash (- = stdin)" name="files" description="file [file...]" required="true" />
  <param switch="md5" count="none" default="0" unspecified="1" value="Calculate MD5" description="md5" typename="bool" />
  <param switch="sha256" count="none" default="0" unspecified="1" value="Calculate SHA256" description="md5" typename="bool" />
  <param switch="sha1" count="none" default="1" unspecified="1" value="Calculate SHA1 (default)" description="sha1" typename="bool?" />
  <?h using System.Security.Cryptography; ?>
  <foreach in="${files}">
    <set hash="${= $md5 ? MD5.Create() : null }" />
    <set hash="${= $sha256 ? SHA256.Create() : $hash }" />
    <set hash="${= $hash ?? SHA1.Create()  }" />

    <set stream="${= ($==&quot;-&quot;)?System.Console.OpenStandardInput():c.OpenStream($)}" />
    <try>
        <print>${=.ToHex($hash.ComputeHash($stream)).ToUpper()}</print>
    </try>
    <finally>
        <eval>$stream.Dispose();</eval>
    </finally>
  </foreach>
</xsharper>