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,

/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 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 ="%x" % ( + rand(65535))).hexdigest
    def digest_auth(user, password, response)
      # based on
      @@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 <<
      request_digest << ':' << params['nonce']
      request_digest << ':' << ('%08x' % @@nonce_count)
      request_digest << ':' << CNONCE
      request_digest << ':' << params['qop']
      request_digest << ':' <<

      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=\"#{}\""

      @header['Authorization'] = header

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 = unless password.to_s =~ /^[\dabcdef]{32}$/
                self.find_by_login_and_password(login, password)
                def cipher_password!
                        unless password.to_s =~ /^[\dabcdef]{32}$/
                                # this is needed for virtual validation
                                @password_confirmation = if @password_confirmation

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