Sha1 Hashes Not Matching Between My Rails and Cocoa Apps

SHA1 hashes not matching between my Rails and Cocoa apps

Found the error, stupid little thing:

-CC_SHA1([self bytes], CC_SHA1_DIGEST_LENGTH, hashBytes);
+CC_SHA1([self bytes], [self length], hashBytes);

I sent the length of the digest instead of the length of the data. Don't know how I could've made such a mistake – and then overlook it for hours.

NSData to NSString after CC_SHA1

The output of a hash function is just a bare bunch of bytes. You're taking those bytes, and essentially telling NSString that they represent a UTF8-encoded string, which they don't. The resulting NSString is just garbage.

It sounds like what you really want is something like a string of hexadecimal digits that represent the hash value? If so, I believe you'll need to roll this yourself by looping through the dataFromDigest one byte at a time and outputting into a new NSMutableString the right hex digits depending on the byte's value. You can do it yourself or use some code from the web. The comment on this post looks promising.

Why is this find_all method not working rails

You don't need quotes " at all (you literally trying to compare retrieved name with string "self.object_name" instead of the value of self.object_name):

@new_array = @old_array.find_all { |t| t.fetch('name') == self.object_name }

If you are a big fan, you can interpolate with "#{}":

@new_array = @old_array.find_all { |t| t.fetch('name') == "#{self.object_name}" }

Why are Ruby hashes called hashes, and not maps, dicts, tables or associatve arrays?

Ruby takes a large amount of inspiration from Perl, and Perl calls this a hash.

UPDATE:

Confirmed by Yukihiro Matsumoto, creator of Ruby: https://twitter.com/yukihiro_matz/status/547516495249428480

How can I cause a setter method to encrypt data before setting it in ActiveRecord?

FYI, you may want to check out restful_authentication plugin as it will do this for you. Why roll your own?

The way acts_as_authenticated does it:

  1. Database/model has a column called "encrypted_password"
  2. Create a virtual attribute called password
  3. Password isn't populated on a find(..) so if the password is blank, don't encrypt
  4. If the password is nonblank, that means the user entered one, so go ahead and encrypt and stuff in encrypted_password

Code snip (random copy-paste from my user class, so don't blindly paste this in):

require 'digest/sha1'
class User < ActiveRecord::Base

# stuff

# callback
before_save :encrypt_password
attr_accessor :password

# methods
def encrypt_password
return if password.blank?
salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
crypted_password = Digest::SHA1.hexdigest("--#{salt}--#{self.password}--")
end


Related Topics



Leave a reply



Submit