Anonymous user
SHA-256: Difference between revisions
→{{header|Racket}}: More compact code
(→{{Header|Python}}: Python 3 as well as 2) |
(→{{header|Racket}}: More compact code) |
||
Line 429:
=={{header|Racket}}==
<
#lang racket/base
(only-in file/sha1 bytes->hex-string))▼
;; define a quick SH256 FFI interface, similar to the Racket's default
;; SHA1 interface
(require ffi/unsafe ffi/unsafe/define openssl/libcrypto
(define-ffi-definer defcrypto libcrypto)
(
(defcrypto SHA256_Update (_fun _pointer _pointer _long -> _int))
(defcrypto SHA256_Final (_fun _pointer _pointer -> _int))
(SHA256_Update ctx bytes (bytes-length bytes))
▲(define (sha256-bytes in)
▲ (let ([ctx (malloc 128)]
▲ [result (make-bytes 32)])
▲ (SHA256_Init ctx)
▲ (SHA256_Final result ctx)
▲ (bytes->hex-string (sha256-bytes in)))
;; use the defined wrapper to solve the task
(displayln (sha256 #"Rosetta code"))
</lang>
{{out}}
|