crypto.lsp sha256

Started by DavMin, December 09, 2011, 12:44:28 PM

Previous topic - Next topic

DavMin

I'm trying to port over my php code dealing with amazon web service to newlisp:
$signature = base64_encode(hash_hmac("sha256",$request,SECRET_ACCESS_KEY,TRUE));

Looking at http://www.newlisp.org/code/modules/crypto.lsp.html">//http://www.newlisp.org/code/modules/crypto.lsp.html there doesn't seem to be sha256. Reading http://newlispfanclub.alh.net/forum/viewtopic.php?f=16&t=2701&p=15122&hilit=sha256#p15122">//http://newlispfanclub.alh.net/forum/viewtopic.php?f=16&t=2701&p=15122&hilit=sha256#p15122 it seems this was noticed.



Thanks!

Lutz

#1
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

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

DavMin

#3
Aha. I see thank you for the help!

hilti

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




(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 "00" 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">//http://en.wikipedia.org/wiki/SHA-256 it seems to work.



(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\">http://dragonfly.apptruck.de