Never been to TextSnippets 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 (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
4 total  XML / RSS feed 

Use md5 to generate website passwords

Use the following command in your shell to generate unique passwords for websites based on a master password. md5 generates a hash of the text string 'SECRET:domain.tld'. awk and cut retrieve first 10 characters of said hash. 'SECRET' would be your master password. 'domain.tld' would be the site in question (eg, textsnippets.com).

/sbin/md5 -s 'SECRET:domain.tld' | awk '{print $4}' | cut -c 1-10

md5 hash of text string using OSX command line

// md5 Hash using OSX Command Line

/sbin/md5 -s 'secret'

Authenticated Digest for Net:HTTPHeader

Allows authenticated requests to be made.

Based off of http://theexciter.com/articles/bingo and updated for Ruby 1.8.6

Use by calling

    req.digest_auth(username, password, res)


on an HTTP request object before asking for a response.

# net_digest_auth.rb
require 'digest/md5'
require 'net/http'

module Net
  module HTTPHeader
    @@nonce_count = -1
    CNONCE = Digest::MD5.new.update("%x" % (Time.now.to_i + rand(65535))).hexdigest
    def digest_auth(user, password, response)
      # based on http://segment7.net/projects/ruby/snippets/digest_auth.rb
      @@nonce_count += 1

      response['www-authenticate'] =~ /^(\w+) (.*)/

      params = {}
      $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }

      a_1 = "#{user}:#{params['realm']}:#{password}"
      a_2 = "#{@method}:#{@path}"
      request_digest = ''
      request_digest << Digest::MD5.new.update(a_1).hexdigest
      request_digest << ':' << params['nonce']
      request_digest << ':' << ('%08x' % @@nonce_count)
      request_digest << ':' << CNONCE
      request_digest << ':' << params['qop']
      request_digest << ':' << Digest::MD5.new.update(a_2).hexdigest

      header = []
      header << "Digest username=\"#{user}\""
      header << "realm=\"#{params['realm']}\""
      
      header << "qop=#{params['qop']}"

      header << "algorithm=MD5"
      header << "uri=\"#{@path}\""
      header << "nonce=\"#{params['nonce']}\""
      header << "nc=#{'%08x' % @@nonce_count}"
      header << "cnonce=\"#{CNONCE}\""
      header << "response=\"#{Digest::MD5.new.update(request_digest).hexdigest}\""

      @header['Authorization'] = header
    end
  end
end

Automatically cipher user password

This allows you to cipher the password of the user automatically upon entry. This way you can allow entry of raw ciphered password via web services etc. as well as keep it ciphered in the database (for instance as a char(32))

require 'md5'
class User < ActiveRecord::Base
        before_save :cipher_password!
    def self.login(login, password)
                password = MD5.new(password).to_s unless password.to_s =~ /^[\dabcdef]{32}$/
                self.find_by_login_and_password(login, password)
        end
        
        private
                def cipher_password!
                        unless password.to_s =~ /^[\dabcdef]{32}$/
                                write_attribute("password", MD5.new(password).to_s)
                                # this is needed for virtual validation
                                @password_confirmation = MD5.new(@password_confirmation).to_s if @password_confirmation
                        end
                end
end


« Newer Snippets
Older Snippets »
4 total  XML / RSS feed