<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: sha1 code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 20:15:12 GMT</pubDate>
    <description>DZone Snippets: sha1 code</description>
    <item>
      <title>MD5 and SHA1 in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3349</link>
      <description>// A Simple MD5/SHA1 calculator in Ruby&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby1.8 -w&lt;br /&gt;&lt;br /&gt;#&lt;br /&gt;# A Simple Hash Code Calculator&lt;br /&gt;# Returns SHA1 and MD5 hash for any given file&lt;br /&gt;# Author: Alessio Saltarin&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;require 'digest/md5'&lt;br /&gt;require 'digest/sha1'&lt;br /&gt;&lt;br /&gt;$BUFLEN = 1024&lt;br /&gt;&lt;br /&gt;class Hasher&lt;br /&gt;	# Constructor&lt;br /&gt;	# method = "SHA1" or "MD5"&lt;br /&gt;	# filepath = Full filepath&lt;br /&gt;	def initialize(method, filepath)&lt;br /&gt;		if (method.upcase == "-SHA1")&lt;br /&gt;			@hashfunc = Digest::SHA1.new&lt;br /&gt;			@hashname = "SHA1"&lt;br /&gt;		else&lt;br /&gt;			@hashfunc = Digest::MD5.new&lt;br /&gt;			@hashname = "MD5"&lt;br /&gt;		end&lt;br /&gt;		@fullfilename = filepath&lt;br /&gt;	end&lt;br /&gt;&lt;br /&gt;	def hashname&lt;br /&gt;		@hashname&lt;br /&gt;	end&lt;br /&gt;&lt;br /&gt;	# Compute hash code&lt;br /&gt;	def hashsum&lt;br /&gt;		open(@fullfilename, "r") do |io|&lt;br /&gt;			puts "Reading "+@fullfilename&lt;br /&gt;			counter = 0&lt;br /&gt;			while (!io.eof)&lt;br /&gt;				readBuf = io.readpartial($BUFLEN)&lt;br /&gt;				putc '.' if ((counter+=1) % 3 == 0)&lt;br /&gt;				@hashfunc.update(readBuf)&lt;br /&gt;			end&lt;br /&gt;		end&lt;br /&gt;		return @hashfunc.hexdigest&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def usage&lt;br /&gt;	puts "Usage: Hasher.rb [-SHA1|-MD5] filename"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def printresult(filename, method, sum)&lt;br /&gt;	puts "\n" + filename + " ==&gt; "+ method + ": " + sum	&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#Program starts&lt;br /&gt;if (ARGV.length == 2)&lt;br /&gt;	hashcomp = Hasher.new(ARGV[0], ARGV[1])&lt;br /&gt;	printresult(ARGV[1], hashcomp.hashname, hashcomp.hashsum)&lt;br /&gt;	&lt;br /&gt;elsif (ARGV.length == 1)&lt;br /&gt;	hashcomp = Hasher.new("-MD5", ARGV[0])&lt;br /&gt;	printresult(ARGV[0], hashcomp.hashname, hashcomp.hashsum)&lt;br /&gt;else&lt;br /&gt;	usage&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 23 Jan 2007 20:53:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3349</guid>
      <author>axsaxs (Alessio Saltarin)</author>
    </item>
    <item>
      <title>Ruby HMAC verifier</title>
      <link>http://snippets.dzone.com/posts/show/2971</link>
      <description>// Ruby script to verify the HMAC of a file or string.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;#&lt;br /&gt;#  Created by Jon (exabrial+hmacruby@gmail.com) on 2006-11-04.&lt;br /&gt;#  Copyright (c) 2006. All rights reserved.&lt;br /&gt;#  Released under MIT License&lt;br /&gt;&lt;br /&gt;require 'openssl'&lt;br /&gt;require "getopt/std"&lt;br /&gt;include OpenSSL&lt;br /&gt;include Digest&lt;br /&gt;&lt;br /&gt;def printhelp&lt;br /&gt;  help=&lt;&lt;end&lt;br /&gt;Purpose: Provides HMAC-SHA1 of a file or string. Text passwords are SHA1 hashed.&lt;br /&gt;Usage: hmac.rb ["string to digest"] [-f (pathtofile)] [-k (pathtokeyfile)]&lt;br /&gt;Options:&lt;br /&gt;  -f (pathtofile) digests a file instead of a string&lt;br /&gt;  -k (pathtokeyfile) does not prompt for key and uses the specified file as a key instead.&lt;br /&gt;end&lt;br /&gt;  puts help&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if ARGV.size &lt; 1&lt;br /&gt;  printhelp&lt;br /&gt;elsif ARGV.size==1&lt;br /&gt;  @plaintext=ARGV.shift&lt;br /&gt;else&lt;br /&gt;  begin&lt;br /&gt;    opt = Getopt::Std.getopts("f:k:")&lt;br /&gt;  rescue Getopt::StdError&lt;br /&gt;    printhelp&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  if opt["f"]&lt;br /&gt;    @plaintext=File.read(opt["f"])&lt;br /&gt;  end&lt;br /&gt;  if opt["k"]&lt;br /&gt;    @key=File.read(opt["k"])&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  printhelp if (!@plaintext&amp;&amp;!@key)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def getkey&lt;br /&gt;  return @key if @key&lt;br /&gt;  print "Please type your key then push enter:"&lt;br /&gt;  return SHA1.new(gets()).to_s&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def main&lt;br /&gt;  hmacd=HMAC.new(getkey(), SHA1.new)&lt;br /&gt;  hmacd.update(@plaintext)&lt;br /&gt;  puts hmacd.to_s&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;main&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 05 Nov 2006 01:54:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2971</guid>
      <author>exabrial (Jon)</author>
    </item>
    <item>
      <title>sha1</title>
      <link>http://snippets.dzone.com/posts/show/2816</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(setq SHA1 (import "libssl.so.4" "SHA1"))&lt;br /&gt;&lt;br /&gt;(join (map (lambda (x) (format "%02x" (&amp; x 0xff))) (unpack (dup "c" 20) (get-string (SHA1 "abc" 3 0)))))&lt;br /&gt;&lt;br /&gt;;; produces "a9993e364706816aba3e25717850c26c9cd0d89d"&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 14 Oct 2006 09:10:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2816</guid>
      <author>frontera000 (bob bae)</author>
    </item>
    <item>
      <title>SHA1 encryption in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/83</link>
      <description>&lt;code&gt;require 'digest/sha1'&lt;br /&gt;sha1 = Digest::SHA1.hexdigest('something secret')&lt;/code&gt;</description>
      <pubDate>Fri, 08 Apr 2005 22:49:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/83</guid>
      <author>hmans (Hendrik Mans)</author>
    </item>
  </channel>
</rss>
