954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I need a php equivalent to digest::sha1.hexdigest

I need to replicate the encryption shown below:

def self.encrypt(password, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end


I am trying something like this to no avail:

$salt = 'edc93eaf81aa1d64368c35213f192bb4ea81d20d';
$password = $_POST['input'];
$password = sha1($salt.$password);
		
echo "password sha1 value: " . $password;
xylude
Junior Poster
123 posts since May 2008
Reputation Points: 13
Solved Threads: 11
 

Or even if anyone could help me with what "--#" and "--" means in ruby it would help loads. I'm assuming that anything wrapped in curly brackets '{}' is a variable.

xylude
Junior Poster
123 posts since May 2008
Reputation Points: 13
Solved Threads: 11
 

I need to replicate the encryption shown below:

def self.encrypt(password, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

I am trying something like this to no avail:

$salt = 'edc93eaf81aa1d64368c35213f192bb4ea81d20d';
$password = $_POST['input'];
$password = sha1($salt.$password);
		
echo "password sha1 value: " . $password;


Ruby on rails has a built in method and if I remember it correctly it is self.salt . You can always google ruby on rails salt if you'd like.

tiger86
Posting Pro
548 posts since Feb 2008
Reputation Points: 48
Solved Threads: 11
 

@xylude, In response to your question about what -- and #{} mean in Ruby, in your example those hyphens or dashes are just part of a string. "--" is just two hyphens, not ruby code. The important part is in the #{}. The pound sign followed by curly braces is used for interpolating variables and evaluating expressions within strings. E.g.

var1 = "some string"
var2 = "String then interpolating a variable #{ var1 }"

In addition to interpolating variables, you can also evaluate expressions inside the #{ }. E.g.

var = "some string"
expression_in_string = "Var reversed: #{ var.reverse}, now in caps: #{ var.upcase }"
ruby_rocks
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

@xylude, what exactly is failing in your php code? It works fine for me using the php -a interactive shell on Macbook Pro, Leopard. Maybe try pasting your entire php function code into the thread?

ruby_rocks
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You