#!/usr/bin/ruby # Copyright 2014 Oracle | BlueKai require 'digest' ''' Demonstration on email and phone hashing ''' class Hashing def normalizePhoneNumber(phoneNumber) phoneNumber.strip.gsub(/^0+/, "").gsub(/[^0-9]+/, "") end def normalizeEmail(email) email.strip.downcase.gsub(/\+[^@]*@/, "@") end def createHash(stringToHash, algorithm) algorithms = { 'sha256'=> lambda { |s| Digest::SHA256.hexdigest(s.to_s)} } procedure = algorithms[algorithm] raise "#{algorithm} algorithm is not supported" if procedure.nil? procedure.call stringToHash end def createEmailHash(email, algorithm) createHash normalizeEmail(email), algorithm end def createPhoneHash(phone, algorithm) createHash normalizePhoneNumber(phone), algorithm end end if __FILE__ == $0 def assert(actual, expected) raise "'#{actual}' is expected to be equal to '#{expected}'" if actual != expected end hashCreator = Hashing.new assert hashCreator.normalizeEmail(" Test@somewhere.com "), "test@somewhere.com" assert hashCreator.normalizeEmail("Test+alias@gMail.Com "), "test@gmail.com" assert hashCreator.normalizePhoneNumber(" 00123-456-7890 "), "1234567890" assert hashCreator.createEmailHash(" Test@somewhere.com ", "sha256"), "c6835820412cddddd5197dab400fec65c3d9a2617ae4ceb1442ec753abeec0ba" assert hashCreator.createPhoneHash(" 00123-456-7890 ", "sha256"), "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646" puts "All tests pass!" end