Waiting for Singularity

Thoughts on science, tech and other preoccupations

Decode This Message - Part 2

Working with MD5 hashes

Take a crack at decoding another secret message:

Here is an MD5 hash, b354e9b8c7a5fb1e073670a28f957032. You need to write a ruby program that uses brute force to determine the String value of the hash. Whoever gets the value, along with a working Ruby program, first wins a delicious prize!

Hints: All lowercase letters, No spaces, No underscores, No numbers, No characters other than letters a-z, 6 characters long

According to The Ruby Way: Solutions and Techniques in Ruby Programming, Third Edition: “The MD5 message-digest algorithm produces a 128-bit fingerprint or message digest of a message of arbitrary length. This is in the form of a hash, so the encryption is one-way and does not allow for the discovery of the original message from the digest.”

MD5 hashes have been used for encoding passwords, credit cards numbers and other sensitive data into databases. An MD5 hash is NOT encryption but is a one-way transaction; for sufficiently long strings, it is almost impossible to reverse engineer an MD5 hash to retrieve the original.

Sounds like a challenge!

Solution in Ruby (1)

require 'digest/md5'

def make_word
  word = []
  letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
  6.times do
    word << letters.sample
  end
  return word.join().to_s
end

def make_hash(word)
  Digest::MD5.hexdigest(word)
end

def brute_force(key)
  message = make_word
  coded_message = make_hash(message)

  if coded_message == key
    puts "The message is #{message}"
    return false
  end
end

while true
  brute_force('b354e9b8c7a5fb1e073670a28f957032')
end

Solution in Ruby (2)

require 'digest/md5'
key = 'b354e9b8c7a5fb1e073670a28f957032'

def make_words
  word_list = []
  letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
  words = letters.permutation(6).to_a
  words.each do |word|
    word_list << word.join()
  end
  return word_list
end

def make_hash(word)
  Digest::MD5.hexdigest(word)
end

def brute_force(key)
  word_list = make_words
  while true
    word_list.each do |message|
      coded_message = make_hash(message)
      if coded_message == key
        puts "The message is #{message}"
        return false
      end
    end
  end
end

brute_force('b354e9b8c7a5fb1e073670a28f957032')

Output

$ ruby md5_decoder.rb
The message is mangia

Bonus: The 5-second solution

Chances are your hash has been cracked at MD5Decrypter with its database of 43 billion unique decrypted MD5 hashes.

Thanks to Eric Kelly of Launch Academy for the original MD5 challenge which can be found in this gist: Secret Prize.

Thinking Inside the Box

21st Century motion control” pretty much sums it up:

“Box” explores the synthesis of real and digital space through projection-mapping on moving surfaces. The short film documents a live performance, captured entirely in camera. Bot & Dolly produced this work to serve as both an artistic statement and technical demonstration. It is the culmination of multiple technologies, including large scale robotics, projection mapping, and software engineering. We believe this methodology has tremendous potential to radically transform theatrical presentations, and define new genres of expression.

How Young Is Too Young to Begin Coding?

Children have a knack for learning new languages. Could it be true for programming languages as well?

  1. Paul Gibson had read educational psychology that suggested it was futile to try to reach children who had yet to achieve literacy. But when he made experimental forays into classrooms of 5- and 6-year-olds, the kids grasped his lessons with surprising ease.

With the help of a custom Java applet, he was able to get kindergartners to write a tic-tac-toe program, based on step-by-step rules the students formulated as a group. And using colored balls and string, he taught the kids how to create graph algorithms, an essential component of computer science. “We believe our work shows that you can start teaching computer science before students even know how to read and write,” Gibson (who now teaches in France) wrote in a 2012 paper.

“Children aged from 5-11 have so much potential for learning about algorithms and computation that it would be a shame to wait until they are teenagers before we teach them the foundations.” That notion is still too radical for most educators. While a popular movement is afoot to teach children, including adolescents, to code—touted by the likes of Bill Gates and basketball star Chris Bosh—few people believe that kindergartners can learn how to bend machines to their will.

Read the full article on Wired.

Applying TDD in Writing Bioinformatics Software

Ruby for Biologists – Part 1

DNA sequence analysis is a basic task in molecular biology and is a good starting point for software development using Test Driven Development (TDD). Given the sequence of a common sequencing primer, “GCAATTAACCCTCACTAAAGG”, I wrote a program to calculate the percentage of each nucleotide. For this, I chose Ruby, a dynamic scripting language and Rspec, a testing tool for the Ruby language that uses simple language to describe expectations on how the code should work.

Starting with a simple example that expresses some basic behaviour, for example ‘it counts the frequency of nucleotides in a string’ and following TDD best practices, I wrote a failing test then wrote just enough Ruby code to make it pass. I then improved the code, added another expected behavior and repeated the cycle. In TDD circles, this is known as Red-Green-Refactor. Primer sequences are typically 18-24 nucleotides, but for genes that are several thousand nucleotides in length, a script that automates the task of calculating DNA statistics can be quite powerful.

Specs

    # dna_stats_spec.rb

    require 'rspec'

    require_relative 'dna_stats'

    describe 'Dna statistics' do
      it 'counts the frequency of nucleotides in a string' do
        expect(Dna.new('agccttaa').count).to eql([["a", 3], \
        ["c", 2], ["g", 1], ["t", 2]])
      end

      it 'counts the frequency of nucleotides in a string' do
        expect(Dna.new('nggaatncc').count).to eql([["a", 2], \
        ["c", 2], ["g", 2], ["n", 2], ["t", 1]])
      end

      it 'analyzes the dna' do
        expect(Dna.new('aaggcccttt').analyze).to eql([["a", 0.2], \
        ["c", 0.3], ["g", 0.2], ["t", 0.3]])
      end

      it 'displays the dna statistics' do
        expect(Dna.new('aaggcccttt').display).to eql("DNA statistics \
        (10 residue sequence) A: 20.0% C: 30.0% G: 20.0% T: 30.0%")
      end
    end

Solution in Ruby

    # dna_stats.rb

    class Dna

      attr_reader :sequence, :sorted_array, :statistics, :string

      def initialize(sequence)

        @sequence = sequence
        @sorted_array = []
        @statistics = []
        @string = ''
      end


      def count
        hash = {}
        sequence.each_char do | char |
          hash[char] ? hash[char] << char : hash[char] = [char]
        end

        hash.each do | key, value |
          hash[key] = value.size.to_i
        end
        #binding.pry
        @sorted_array = hash.sort
        return @sorted_array

      end

      def analyze
        @total_bases = 0
        count
        @sorted_array.each do | item |
          @total_bases += item[1]
        end
        string << "DNA statistics (#{@total_bases} residue sequence)"
        @sorted_array.each do | item |
          @statistics << [item[0], (item[1].to_f / @total_bases)]
        end
        return @statistics
      end

      def display
        analyze
        @statistics.each do | item |
          string << " #{item[0].upcase}: #{(item[1]*100).round(1)}%"
        end
        return string
      end
    end

    sequence = "GCAATTAACCCTCACTAAAGG"
    puts "DNA sequence: " + sequence
    seq = Dna.new(sequence)
    puts seq.display

Output

    ruby-bio-suite $ ruby dna_statistics.rb

    DNA sequence: GCAATTAACCCTCACTAAAGG
    DNA statistics (21 residue sequence) A: 38.1% C: 28.6% G: 14.3% T: 19.0%

Future implementations of this code will include methods for determining melting temperature, Tm and detecting hairpin loops.

Got 15 minutes? Give Ruby a try now: Try Ruby. Learn more about Ruby and Rspec here and here.

JQuery API Flashcards

A flashcard app to test your knowledge of the JQuery API. Question pool based on documentation from jQuery API.

Outputs:

    *** Test your knowledge of jQuery API !***
    Vocabulary based on jQuery API documentation
    http://api.jquery.com/

    Q: Selects the element that is the root of the document.
    A: :root
    Correct
    Enter any key to continue or 'x' to exit: y

    Q: Reduce the set of matched elements to the first in the set.
    A: :first
    Incorrect. The answer is: .first()
    Enter any key to continue or 'x' to exit:

    Q: Selects all next elements matching "next" that are immediately preceded by a sibling "prev".
    A: ("prev" > "next")
    Incorrect. The answer is: ("prev + next")
    Enter any key to continue or 'x' to exit: x

JavaScript Flashcards

A flashcard app to test your knowledge of JavaScript statements. Question pool based on documentation from the friendly folks at Mozilla Developer Network.

Outputs:

    *** Test your knowledge of JavaScript statements!***
    Vocabulary based on vocabulary from Mozilla Developer Network
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

    Q: Declares a read-only named constant.
    A: const
    Correct
    Enter any key to continue or 'x' to exit: r

    Q: Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.
    A: while
    Incorrect. The answer is: if...else
    Enter any key to continue or 'x' to exit: y

    Q: A statement used to group zero or more statements and is delimited by a pair of curly brackets.
    A: block
    Correct
    Enter any key to continue or 'x' to exit: x

Git Adventures - Part 1

  • How to fork a GitHub project
  • Cloning a forked repository and its branches
  • Updating a forked repository

Scenario 1: I want to fork a project on GitHub to work on a feature in my local repository.

    Go to the prject page on GitHub, https://github.com/PROJECT_AUTHOR/PROJECT.git
    Click the "Fork" button
    The project will be added to your list of repositories

Scenario 2: The Github repository has a branch called “bananas”. I want to copy that branch in my local repository.

    git fetch origin
    git checkout --track origin/bananas

Scenario 3: A day later, another branch called “cool-beans” was added to the original project. I want to add that branch in my local repository as well.

    git remote add upstream https://github.com/PROJECT_AUTHOR/PROJECT.git
    git fetch upstream
    git checkout --track upstream/cool-beans

References: GitHub article and StackOverflow discussions here and here

Decode This Message

A lesson in Base64 encoding

A recent hackathon offered a sweet discount to developers who could crack the following encoded message:

    bmljZSBkZWNvZGluZyB5b3VuZyBwYWRhd2FuIFVzZSB5b3UgbXVzdCBsZWFybmVyIGZyZWUgdGlja2V0IGNvZGU6IEhBQ0tUQVNUSUM=

The last character of the message is “=” which points us to the right direction. Base64 encoding uses “=” as the character for padding.

According to Wikipedia:

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

Solution in Python

    >>> import base64
    >>> coded_string = '''bmljZSBkZWNvZGluZyB5b3VuZyBwYWRhd2FuIFVzZSB5b3UgbXVzdCBsZWFybmVyIGZyZWUgdGlja2V0IGNvZGU6IEhBQ0tUQVNUSUM='''
    >>> base64.b64decode(coded_string)
    'nice decoding young padawan Use you must learner free ticket code: HACKTASTIC'

    To encode a message in Ruby: Base64.encode64(message)
    Tested in Python 2.7.2

Solution in Ruby

    2.0.0-p247 :001 > require 'base64'
     => true

    2.0.0-p247 :002 > coded_string = "bmljZSBkZWNvZGluZyB5b3VuZyBwYWRhd2FuIFVzZSB5b3UgbXVzdCBsZWFybmVyIGZyZWUgdGlja2V0IGNvZGU6IEhBQ0tUQVNUSUM="
     => "bmljZSBkZWNvZGluZyB5b3VuZyBwYWRhd2FuIFVzZSB5b3UgbXVzdCBsZWFybmVyIGZyZWUgdGlja2V0IGNvZGU6IEhBQ0tUQVNUSUM="

    2.0.0-p247 :005 > puts Base64.decode64(coded_string)
    nice decoding young padawan Use you must learner free ticket code: HACKTASTIC
     => nil


    To encode a message in Python: base64.b64encode(message)
    Tested in Ruby 2.0.0

More on Base64 encoding in StackOverflow and Ruby-doc.

Tmux Key Bindings Flashcards

A flashcard app to test your knowledge of Tmux key bindings. Question pool based on the Tmux manual pages from http://www.openbsd.org/.

Outputs:

*** Test your knowledge of Ruby Globals!***
Based on the manual pages from http://www.openbsd.org/

Q: What tmux key binding does the following: Force redraw of the attached client
A: a
Incorrect. The answer is: r
Do you want to continue? (Y/N): y

Q: What tmux key binding does the following: Break the current pane out of the window
A: !
Correct
Do you want to continue? (Y/N): y

Q: What tmux key binding does the following: Enter copy mode to copy text or view the history
A: [
Correct
Do you want to continue? (Y/N): n