This page is READ-ONLY. It is generated from the old site.
All timestamps are relative to 2013 (when this page is generated).
If you are looking for TeX support, please go to VietTUG.org

war of php, ruby (rails), and c#

C# shit!
Added by over 3 years ago  »  Votes: 1/1

PHP and C# versions are taken from Facebook Connect. The Ruby version is adapted by Ky Anh.

PHP

PHP code is very readable :)

 1 <?php
 2   /* typical use case: process.php?string=$string&sig=fb_sig in process.php,
 3      $string .= $secret and md5($string) == $_GET['fb_sig'] */
 4 
 5   $fbsig = array();
 6 
 7   foreach($_GET as $key=>$value) {
 8     if(substr($key,0,7) == 'fb_sig_') {
 9       $fbsig[substr($key,7)] = $value;
10     }
11   }
12 
13   ksort($fbsig);
14 
15   foreach($fbsig as $key=>$value) {
16     $string .= $key . '=' . $value;
17   }
18 
19   $string .= $secret;
20 
21   md5($string) == $_GET['fb_sig'];
22 ?>

Ruby (Rails)

This is shorter and more readable than the previous one. Note: params is a Rails variable ( == $_GET in PHP)

 1  fb_sigs = []
 2 
 3  params.each { |k,v| fb_sigs[k] = v if k.match(/^fb_sig_/) }
 4 
 5  fb_sigs = fb_sigs.sort{|a,b| a[0] <=> b[0]}
 6 
 7  tmp_str = fb_sigs.map{|u| "#{u[0][7,u[0].length]}=#{u[1]}" }.join()
 8  tmp_str += fb_secret_key
 9 
10  return Digest::MD5.hexdigest(tmp_str).equal?(params['fb_sig'])

C#

And the last one is the most readable. And I guess that you should buy new glasses.

 1 private bool VerifySignature(System.Collections.Specialized.NameValueCollection nameValueCollection) {
 2   string signature = nameValueCollection["fb_sig"];
 3 
 4   if (String.IsNullOrEmpty(signature)) return false;
 5 
 6   string s = (from key in nameValueCollection.AllKeys
 7        where key.StartsWith("fb_sig_")
 8        orderby key select key.Substring(7)
 9        + "=" + nameValueCollection[key]).Append()
10      + facebookApi.Secret;
11 
12   StringBuilder computedSignature = new StringBuilder();
13 
14   MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(s)).Action(b => computedSignature.AppendFormat("{0:x2}", b));
15 
16   return computedSignature.ToString().ToLowerInvariant() == signature.ToLowerInvariant();
17 }
18 
19 public static string Append(this IEnumerable<string> list) {
20   StringBuilder sb = new StringBuilder();
21   list.Action(s => sb.Append(s));
22   return sb.ToString();
23 }
24 </cod

Comments