crypto.lsp sha256

Q&A's, tips, howto's
Locked
DavMin
Posts: 16
Joined: Fri Dec 09, 2011 8:07 pm

crypto.lsp sha256

Post by DavMin »

I'm trying to port over my php code dealing with amazon web service to newlisp:

Code: Select all

$signature = base64_encode(hash_hmac("sha256",$request,SECRET_ACCESS_KEY,TRUE));
Looking at http://www.newlisp.org/code/modules/crypto.lsp.html there doesn't seem to be sha256. Reading http://newlispfanclub.alh.net/forum/vie ... 256#p15122 it seems this was noticed.

Thanks!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: crypto.lsp sha256

Post by Lutz »

Yes, crypto.lsp, part of the standard modules, has only: md5, sha1, hmac, and ripemd160. But SHA-256 is also part of libcrypto.so , the library imported by crypto.lsp. Perhaps it is easy to add, taking other functions in the module as a template? May be call patterns are identical?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: crypto.lsp sha256

Post by Lutz »

... there is also always the possibility to use the command line with the newLISP 'exec' function with the openssl utility.

DavMin
Posts: 16
Joined: Fri Dec 09, 2011 8:07 pm

Re: crypto.lsp sha256

Post by DavMin »

Aha. I see thank you for the help!

hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Re: crypto.lsp sha256

Post by hilti »

Just a quick hack. I've added this code to crypto.lsp

Code: Select all


(import library "SHA256")

;; @syntax (crypto:sha256 <string> <bool-raw>)
;; @param <string> The string buffer for which to calculate a SHA-256 hash
;; @param <bool-raw> Return the raw binay buffer when 'true'.
;; @return The 32 Byte SHA-1 hash as a 64 Byte long hex string or as a 32 byte binary buffer.
;; @example
;; (crypto:sha256 "ABC") => "3c01bdbb26f358bab27f267924aa2c9a03fcfdb8"
;;
;; (crypto:sha256 (read-file "newlisp-9.1.0.tgz")) => "2127a9c487f338b00f36cfd60b5f33d27b8d0010"
(define (sha256 str raw-flag)
    (if raw-flag
        (let (buff (dup "\000" 32))
            (cpymem (SHA256 str (length str) 0) buff 20)
            buff)
        (join
            (map (lambda (x) (format "%02x" (& x 0xff))) 
                (unpack (dup "c" 32) (SHA256 str (length str) 0)))
        )
    )
)

According to this http://en.wikipedia.org/wiki/SHA-256 it seems to work.

Code: Select all

(println (crypto:sha256 "The quick brown fox jumps over the lazy dog" false))
d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

(println (crypto:sha256 "The quick brown fox jumps over the lazy dog." false))
ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c

Hope this helps a bit in dealing with Amazon.

Cheers
Hilti
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Locked