Welcome to the CHICKEN Scheme pasting service

bear example added by curium on Wed Jun 1 22:46:41 2022

(import (scheme)
        (chicken base)
        (chicken foreign)
        (chicken gc)
        (chicken io)
        (chicken locative)
        (chicken memory)

(foreign-declare "#include \"bearssl.h\"")
;; ^ includes all bearssl headers

(define-foreign-type sha1-context
  (c-pointer "br_sha1_context"))

(define (allocate-sha1-context)
  (allocate (foreign-type-size "br_sha1_context")))

(define (free-sha1-context ctx)
  (free ctx))

(define (make-sha1-context)
  (let ((ctx (allocate-sha1-context)))
    (set-finalizer! ctx free-sha1-context)

(define sha1-init
   void "br_sha1_init" sha1-context))

(define sha1-update
   void "br_sha1_update" sha1-context (const (scheme-pointer void)) size_t))

(define sha1-out
   void "br_sha1_out" (const sha1-context) (scheme-pointer void)))

(define sha1-state
   unsigned-integer64 "br_sha1_state" (const sha1-context) (scheme-pointer void)))

(define sha1-set-state
   void "br_sha1_set_state" sha1-context (const (scheme-pointer void)) unsigned-integer64))

(let* ((ctx (make-sha1-context))
       (len 4096)
       (buf (make-u8vector len))
       (res (make-u8vector 20))
       (b-buf (u8vector->blob/shared buf))
       (b-res (u8vector->blob/shared res)))
  ;; init
  (sha1-init ctx)
  ;; update
  (let loop ()
    (let ((n (read-u8vector! len buf)))
      (when (> n 0)
        (sha1-update ctx b-buf n)
  ;; result
  (sha1-out ctx b-res)
  (print (bytevector->hex-string res)))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Type in the text below:
               _ _    
 _____ __ ___ (_) | __
|_  / '__/ _ \| | |/ /
 / /| | | (_) | |   < 
/___|_|  \___// |_|\_\
Visually impaired? Let me spell it for you (wav file) download WAV