Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Friday, March 4, 2011

Quick and Dirty Sha1 Hash Creation

I had the need to hash a password to get a Sha1 Hash. I've got ruby on my system so I thought I should be able to do that with Ruby. Quick search, found this and did this snippet...
require 'digest/sha1'
sha1 = Digest::SHA1.hexdigest('passwordHerePlease')
Easier than opening Visual Studio, creating a project, inserting this in some function, importing the namespace System.Security.Cryptography, compiling, and running... :)
SHA1CryptoServiceProvider cryptoTransformSHA1 = 
    new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
    cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

P.S. I was wondering how to turn the bytes into a Sha1 like what Ruby outputs. The code above for C# was from here

Wednesday, February 16, 2011

Recursive File Content Search Script in Ruby

Update (2011-02-26): Please note that this program has limitations. For example, currently it will only find one occurence of the search term per line. So if the search term were on a line more than one time you would get incorrect counts. I hope to deal with things like this in a later post.

I've had a couple of situations in the past month where I've wanted to search for a string in every file (essentially) in the application code that I'm working on. In the case today I wanted to find the string "new OleDbCommand(" and count the number of instances.

The code I came up with is as follows:
require 'fileutils'

search_term = "new OleDbCommand("
search_term_count = 0

Dir.glob("**/*.*") do | file_path |
    puts file_path
    
    # Ignore Empty Files
    if File.zero?(file_path) == false    
        File.open(file_path, 'r') do | file |
            file.each_line do | line |
                if line.index(search_term) != nil
                    puts line
                    search_term_count += 1
                end
            end
        end
    end
end

puts
puts "Search Term: '#{search_term}' found #{search_term_count} times."
The most interesting part that I want to point out is the code that gets all of the filenames for me which is line 6. It reads:
Dir.glob("**/*.*") do | file_path |
:
The Dir.glob() function has takes a pattern which is used to tell the function what filename to find. the ** at the start of the pattern tells glob() to do a recursive search through all child directories all the way down as far as it can go. I found this out via a recent post by Rob Connery here. Note at the moment the whole pattern as I have it is not really necessary. The /*.* part just says find all files, but you can change it by changing the * after the dot to specify a type of file (i.e. the file extension) that you want to find. I've put it in there because 1. I started looking for only *.cs files, but then wanted to search globally.

In another post I'm hoping to expand this example to allow this script to do thinks like allowing the search term, search directory, file type and alter some other aspects of the code on the command line when running the script.