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:
- Database/model has a column called "encrypted_password"
- Create a virtual attribute called password
- Password isn't populated on a find(..) so if the password is blank, don't encrypt
- If the password is nonblank, that means the user entered one, so go ahead and encrypt and stuff in encrypted_password
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
Rvm Can No Longer Install 1.8.7-P352 on MAC Os X Mountain Lion
Where Is Ruby's Erb Format "Officially" Defined
How to Test Strong Params with Rspec
Parsing a CSV File Using Different Encodings and Libraries
Why Use Gemspec + Gemfile When Checking for Dependencies
Why Are Global Methods Allowed to Be Defined Outside of a Class in Ruby
Best/Most Elegant Way to Share Objects Between a Stack of Rack Mounted Apps/Middlewares
Is It a Bad Practice to Randomly-Generate Test Data
Access Rake Task Description from Within Task
Foreign Key (Class_Id) Not Populating in Belongs_To Association
Recommended Way to Generate a Presigned Url to S3 Bucket in Ruby
How to Interact with a Caldav Server from Ruby
How to Automatically Escape HTML Content Using Jekyll and Markdown
Why The Unit Test Frameworks in Fortran Rely on Ruby Instead of Fortran Itself
How to Validate Overlapping Times in Rails with Postgresql
Belongs_To Presence in Rails 5 Not Working
Undefined Method 'Merge' for '####':String <%= Form_For %> Helper