[plt-scheme] Accessing openssl from MzScheme, sending control-c (end of text)

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat Jul 7 07:56:19 EDT 2007

At Sat, 7 Jul 2007 13:42:25 +0200, Erich Rast wrote:
> A quick correction:
> You have to write openssl sha1, press return, write some text, press  
> return and then press control-d.
> The question remains the same, though, how can I do this from MzScheme?

Here's a start:

 (define (sha1 str)
   (parameterize ([current-input-port (open-input-string str)])
     (system "openssl sha1")))

But that writes the result to the current output port. So you probably
want this, instead:

 (define (sha1 str)
   (let-values ([(result-in result-out) (make-pipe)])
     (parameterize ([current-input-port (open-input-string str)]
                    [current-output-port result-out])
       (system "openssl sha1"))
    (read-line result-in 'any)))

When you type

 openssl sha1

in a shell, that's the shell command.

>From then on, what you type is going to the "standard input" of the
`openssl' program. When you use `system', then "standard input"
corresponds to the current input port.

In interactive mode, ctl-d ends the standard-input stream. In the above
code, the current input port's stream ends at the end of the string.

The `openssl' programs writes its result to standard output, which
corresponds to the current output port when you use `system'.

If you'd like to use the OpenSSL library more directly, see "mzssl.ss"
in the "openssl" collection that's distributed with PLT Scheme. (We
include a copy of OpenSSL libraries with Windows distribution.)


Posted on the users mailing list.