EasyCFM.COM ColdFusion Forums / PHP to CFML / Here is a fun one!

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: Here is a fun one!

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

mnkyhead
12-10-2007 @ 5:04 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 4
Joined: Dec 2007

it's a sha1 encryption function! Good Times!
If there is another way to do this same thing, that would work to.

# calculates SHA-1 digest of the input string
# cleaned up from John Allen's "SHA in 8 lines of perl5"
# at http://www.cypherspace.org/~adam/rsa/sha.html
#
# returns the hash in a (binary) string

function cybs_sha1($in) {
  $indx = 0;
  $chunk = "";

  $A = array(1732584193, 4023233417, 2562383102,  271733878, 3285377520);
  $K = array(1518500249, 1859775393, 2400959708, 3395469782);
  $a = $b = $c = $d = $e = 0;
  $l = $p = $r = $t = 0;

  do{
    $chunk = substr($in, $l, 64);
    $r = strlen($chunk);
    $l += $r;

    if ($r<64 && !$p++) {
      $r++;
      $chunk .= "\x80";
   }
    $chunk .= "\0\0\0\0";
   while (strlen($chunk) % 4 >
0) {
      $chunk .= "\0";
    }
    $len = strlen($chunk) / 4;
    if ($len > 16) $len = 16;
    $fmt = "N" . $len;
    $W = array_values(unpack($fmt, $chunk));
    if ($r < 57 ) {
      while (count($W) < 15) {
     array_push($W, "\0");
      }
      $W[15] = $l*8;
   }

    for ($i = 16; $i <= 79; $i++) {
      $v1 = d($W, $i-3);
      $v2 = d($W, $i-8);
      $v3 = d($W, $i-14);
      $v4 = d($W, $i-16);
      array_push($W, L($v1 ^ $v2 ^ $v3 ^ $v4, 1));
    }

    list($a,$b,$c,$d,$e)=$A;

   for ($i = 0; $i<=79; $i++) {
      $t0 = 0;
     switch(intval($i/20)) {
     case 1:
     case 3:
     $t0 = F1($b, $c, $d);
     break;
     case 2:
     $t0 = F2($b, $c, $d);
     break;
      default:
     $t0 = F0($b, $c, $d);
     break;
      }
      $t = M($t0 + $e  + d($W, $i) + d($K, $i/20) + L($a, 5));
      $e = $d;
     $d = $c;
     $c = L($b,30);
     $b = $a;
     $a = $t;
   }

    $A[0] = M($A[0] + $a);
    $A[1] = M($A[1] + $b);
    $A[2] = M($A[2] + $c);
    $A[3] = M($A[3] + $d);
    $A[4] = M($A[4] + $e);

  }while ($r>
56);
  $v = pack("N*", $A[0], $A[1], $A[2], $A[3], $A[4]);
  return $v;
}

#### Ancillary routines used by sha1

function dd($x) {
  if (defined($x)) return $x;
  return 0;
}

function d($arr, $x) {
  if ($x < count($arr)) return $arr[$x];
  return 0;
}

function F0($b, $c, $d) {
  return $b & ($c ^ $d) ^ $d;
}

function F1($b, $c, $d) {
  return $b ^ $c ^ $d;
}

function F2($b, $c, $d) {
  return ($b | $c) & $d | $b & $c;
}

# ($num)
function M($x) {
  $m = 1+~0;
if ($m == 0) return $x;
  return($x - $m * intval($x/$m));
}

# ($string, $count)
function L($x, $n) {
  return ( ($x<<$n) | ((pow(2, $n) - 1) & ($x>
>(32-$n))) );
}

####
#### end of HMAC SHA1 implementation #####


Webmaster
12-10-2007 @ 5:29 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4542
Joined: Jan 2002

Since Sha1 is built into java; you can do this:

<!--- Define a sharedKey (Put in your own VALID key) --->
<cfset SharedKey = "" />
<!--- Define a string to encrypt --->
<cfset PlainText = "" />
<!--- create a java SHA1 object --->
<cfset oHmacSha = CreateObject("java",  "HmacSha")>
<!--- now you can encrypt it --->
<cfset EncryptedText = oHmacSha.TextEncrypt(SharedKey, PlainText)>

That should accomodate it easier and about the same... let me know if you have any questions.

Pablo Varando
Senior Application Architect
EasyCFM.COM, LLC.

904.483.1457 \\ mobile
webmaster@easycfm.com \\email

mnkyhead
12-11-2007 @ 9:49 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 4
Joined: Dec 2007

Thanks Pablo!
So I have 2 questions.
will this work with coldfusion 6?
And for the shared key, can it be any string?

Thanks
Russ


Website Designed and Developed by Pablo Varando.