[racket] Example of using OpenSSL instead of mzcrypto

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Apr 24 17:12:18 EDT 2012

An hour and a half ago, Neil Van Dyke wrote:
> I'd put those port close calls in a "dynamic-wind" cleanup thunk.
> I'd also catch the exceptions that could be raised by the
> port-closing procedure itself in the cleanup thunk.  And I'd make
> sure that any custodian I was using didn't close the ports at the
> wrong time for me.

More conveniently:

  (with-output-to-string (λ() (system "whatever")))

> Regarding I/O, I'd make stdin a null port so that the process wouldn't 
> block (unless I actually had to feed it input).  [...]


  (parameterize ([current-input-port (open-input-string "")])
    (with-output-to-string (λ() (system "whatever"))))

> I also prefer to use "process*" over "process", because it
> eliminates some possible command line assembling errors (especially
> quoting and escaping) and eliminates resulting exploits.  (I'll just
> assert here that most people who assemble command lines as single
> strings using variables do it wrong and potentially create security
> exploits or accidents.)

And that's a very good point -- IMO, string concatenation is good only
for quick uses with a known simple string (ie, no uses that assemble

I often end up with a little utility for that, something like:

  (define (make-exe name)
    (define (->string x) (if (string? x) x (format "~a" x)))
    (define exe (find-executable-path (->string name)))
    (unless exe (error 'make-exe "could not find executable: ~e" name))
    (λ args (apply system* exe (map ->string args))))
  (define-syntax-rule (define-exe name) (define name (make-exe 'name)))
  (define-exe echo)
  (echo 1 'foo "bar")

And that can be extended with keywords as needed, for returning a
string, cleaning it from it's terminating newline and/or splitting by
newlines, having an empty input, running asynchronously with a
callback, specifying input/output files, etc etc etc.

(It's probably a good idea to add something like that to
`racket/system', but the huge number of possibilities made it easier
to just write a specific wrapper whenever I need one...)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.