nunojob:~ dscape/08$ echo The Black Sheep

Posts tagged ‘Ruby’

RSA Encrypt & Decrypt in ruby

Well I finished the encrypt with RSA on ruby some hours ago and felt like sharing :)

Case you feel like doing something back for me just download the latest release of my beta twitter client and send me some comments to my email. It’s pretty hard to test something when my environment is completely contaminated !

require 'openssl'
require 'Base64'

class Rudolph
  class Crypt
    def initialize data_path
      @data_path = data_path
      @private   = get_key 'id_rsa'
      @public    = get_key 'id_rsa.pub'
    end

    def encrypt_string message
      Base64::encode64(@public.public_encrypt(message)).rstrip
    end

    def decrypt_string message
      @private.private_decrypt Base64::decode64(message)
    end

    def self.generate_keys data_path
      rsa_path = File.join(data_path, 'rsa')
      privkey  = File.join(rsa_path, 'id_rsa')
      pubkey   = File.join(rsa_path, 'id_rsa.pub')
      unless File.exists?(privkey) || File.exists?(pubkey)
        keypair  = OpenSSL::PKey::RSA.generate(1024)
        Dir.mkdir(rsa_path) unless File.exist?(rsa_path)
        File.open(privkey, 'w') { |f| f.write keypair.to_pem } unless File.exists? privkey
        File.open(pubkey, 'w') { |f| f.write keypair.public_key.to_pem } unless File.exists? pubkey
      end
    end

    private
    def get_key filename
      OpenSSL::PKey::RSA.new File.read(File.join(@data_path, 'rsa', filename))
    end
  end
end
Advertisements

Rudolph – Yet another Twitter client

I felt like trying the Shoes framework. Here are the results so far. When I get some free time I’ll try to post some guidelines to do something like this.

Webpage: http://the-shoebox.org/apps/105

Github: http://github.com/dscape/rudolph

Open Source Online Testing System (OSOTS)

I simply can’t believe that I forgot to post this project in my blog. Anyway if you have to deploy a multiple choice test and you feel like messing arround with Ruby on Rails and DB2 here goes the link.

Open Source Online Testing System (OSOTS)

Cuidado com os tigres

Mais uma excelente talk do Steve Yegge.

Scala, now, is the tiger that’s going to kill Java. Because their [type-talisman] argument now has become a paradox, similar to the Paul Graham Blub Paradox thing, right? Because they’re like, “Well, we need static typing in order to engineer good systems. It’s simply not possible otherwise.”

The Scala people come in and they go: “Your type system suuuuuucks. It’s not sound. It’s not safe. It’s not complete. You’re casting your way around it. It doesn’t actually prevent this large class of bugs. How many times have you written catch (NullPointerException x) ... in Java? Our type system doesn’t allow [things like] that.”

Our type system does what you said your type system was doing.

So, therefore, you should be using it! ∴

And the Java people look at it and go: “Wehellll… (cough cough)… I mean, yeah, I mean… (*ahem*)(running finger under collar, as if sweating profusely) They say, “Welllll… you know… it’s awfully… cummmmmbersome… I…”

“We can actually get around the problems in practice that you guys say your type system is solving through Good Engineering Practices.”

(laughter begins to grow)

HA!!! (I point accusingly at the audience, and there’s more laughter)

Yeah.

So Scala is creating a real problem for [Java’s] static typing camp now. Because their last little bastion of why they’re using it, the whole tigers argument, they’re like, “Ah, well… we… we keep shotguns in our house.” [This is what they’ve been reduced to.]

OK? Yeeeeahhhh…

So back to dynamic languages!

Ruby Ruby Ruby Ruby

Parece que fui ao rock in rio ver kaiser chiefs (e fui).

Estava para aqui a brincar ao ruby e assim do nada encontro-me a chamar métodos dinamicamente (ruby as a meta-language) que devolviam strings. Tudo bem até chegar a um que devolvia integer e eis que aparece aquele erro:

NoMethodError in SearchController#search

undefined method `include?’ for 7:Fixnum

Que fazer? Mudar aquela linha de código por um if e contemplar as duas possibilidades? Ou simplesmente alterar o Fixnum e ensinar-lhe o que eu esperava que o include? fizesse? Eu fui pela segunda opção:


class Fixnum
  def include?(object)
    to_i == object.to_i
  end
end

Em linguagens assim dá gosto trabalhar!

Wouldn’t ANTLR be better if written in a dynamic language?

I think making a tool like ANTLR is a non-dynamic language is a shot in the foot!

It just makes sense to use dynamic languages to create meta-language tools. Would facilitate the work of the developer and of the ones writing grammars taking away all that java trash that helps in nothing to make out stuff work. ANTLR is great, but all the negative aspects (from end-user point of view) arise from the fact of having all that unnecessary information and needing to declare types and other stuff that ARE NOT NATURAL for a grammar to have. And the way it’s implemented in Java is just a way of copying the way (bad) dynamic languages work.

grammar Expr; 

options {
  output = AST;
  ASTLabelType = CommonTree;
} 

prog
     : ( stat
         { System.out.println( $stat.tree.toStringTree() ); }
       )+
     ;

stat
     : expr NEWLINE -> expr
     | ID '=' expr NEWLINE -> ^('=' ID expr)
     | NEWLINE ->
     ; 

expr
     : multExpr ( ( '+'^ | '-'^ ) multExpr )*
     ;

multExpr
     : atom ( '*'^ atom )*
     ;

atom
     : INT
     | ID
     | '('! expr ')'!
; 

ID
     : ( 'a'..'z' | 'A'..'Z' )+
     ;

INT
     : '0'..'9'+
     ; 

NEWLINE
     : '\r'? '\n'
     ; 

WS
     : ( ' ' | '\t' | '\n' | '\r' )+
       { $channel=HIDDEN; }
     ;
grammar Expr; 

options {
  output = AST;
} 

prog
     : ( stat
         { puts $stat.to_tree }
       )+
     ;

stat
     : expr NEWLINE
     | ID '=' expr NEWLINE -> ^ // mark the root somehow
     | NEWLINE
     ; 

expr
     : multExpr ( ( '+'^ | '-'^ ) multExpr )*
     ;

multExpr
     : atom ( '*'^ atom )*
     ;

atom
     : INT
     | ID
     | '('! expr ')'!
; 

ID
     : ( 'a'..'z' | 'A'..'Z' )+
     ;

INT
     : '0'..'9'+
     ; 

NEWLINE
     : '\r'? '\n'
     ; 

WS
     : ( ' ' | '\t' | '\n' | '\r' )+
       { skip }
     ;

Just wish I had the time to implement it in Ruby and prove my point! Maybe in the masters thesis..

By the way, the ANTLR Book is great. If you want to learn about grammars and creating DSL’s you will love it. But be advised, you’ll hate the java trash that comes with it.

By the way Google changed their icon.

But once again, who gives a fuck? I sure don’t.

Information Retrieval using the Boolean Model in Ruby on Rails