Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

MD5 and SHA1 in Ruby

// A Simple MD5/SHA1 calculator in Ruby

#!/usr/bin/ruby1.8 -w

#
# A Simple Hash Code Calculator
# Returns SHA1 and MD5 hash for any given file
# Author: Alessio Saltarin
#

require 'digest/md5'
require 'digest/sha1'

$BUFLEN = 1024

class Hasher
	# Constructor
	# method = "SHA1" or "MD5"
	# filepath = Full filepath
	def initialize(method, filepath)
		if (method.upcase == "-SHA1")
			@hashfunc = Digest::SHA1.new
			@hashname = "SHA1"
		else
			@hashfunc = Digest::MD5.new
			@hashname = "MD5"
		end
		@fullfilename = filepath
	end

	def hashname
		@hashname
	end

	# Compute hash code
	def hashsum
		open(@fullfilename, "r") do |io|
			puts "Reading "+@fullfilename
			counter = 0
			while (!io.eof)
				readBuf = io.readpartial($BUFLEN)
				putc '.' if ((counter+=1) % 3 == 0)
				@hashfunc.update(readBuf)
			end
		end
		return @hashfunc.hexdigest
	end
end

def usage
	puts "Usage: Hasher.rb [-SHA1|-MD5] filename"
end

def printresult(filename, method, sum)
	puts "\n" + filename + " ==> "+ method + ": " + sum	
end

#Program starts
if (ARGV.length == 2)
	hashcomp = Hasher.new(ARGV[0], ARGV[1])
	printresult(ARGV[1], hashcomp.hashname, hashcomp.hashsum)
	
elsif (ARGV.length == 1)
	hashcomp = Hasher.new("-MD5", ARGV[0])
	printresult(ARGV[0], hashcomp.hashname, hashcomp.hashsum)
else
	usage
end

Ruby HMAC verifier

// Ruby script to verify the HMAC of a file or string.

#!/usr/bin/env ruby
#
#  Created by Jon (exabrial+hmacruby@gmail.com) on 2006-11-04.
#  Copyright (c) 2006. All rights reserved.
#  Released under MIT License

require 'openssl'
require "getopt/std"
include OpenSSL
include Digest

def printhelp
  help=<<end
Purpose: Provides HMAC-SHA1 of a file or string. Text passwords are SHA1 hashed.
Usage: hmac.rb ["string to digest"] [-f (pathtofile)] [-k (pathtokeyfile)]
Options:
  -f (pathtofile) digests a file instead of a string
  -k (pathtokeyfile) does not prompt for key and uses the specified file as a key instead.
end
  puts help
  exit
end

if ARGV.size < 1
  printhelp
elsif ARGV.size==1
  @plaintext=ARGV.shift
else
  begin
    opt = Getopt::Std.getopts("f:k:")
  rescue Getopt::StdError
    printhelp
  end
  
  if opt["f"]
    @plaintext=File.read(opt["f"])
  end
  if opt["k"]
    @key=File.read(opt["k"])
  end
  
  printhelp if (!@plaintext&&!@key)
end

def getkey
  return @key if @key
  print "Please type your key then push enter:"
  return SHA1.new(gets()).to_s
end

def main
  hmacd=HMAC.new(getkey(), SHA1.new)
  hmacd.update(@plaintext)
  puts hmacd.to_s
end

main

sha1

// description of your code here

(setq SHA1 (import "libssl.so.4" "SHA1"))

(join (map (lambda (x) (format "%02x" (& x 0xff))) (unpack (dup "c" 20) (get-string (SHA1 "abc" 3 0)))))

;; produces "a9993e364706816aba3e25717850c26c9cd0d89d"

SHA1 encryption in Ruby

require 'digest/sha1'
sha1 = Digest::SHA1.hexdigest('something secret')
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS