#lang scheme/base
(require (planet bzlib/base)
(planet vyzo/crypto)
(planet vyzo/crypto/util)
"filter.ss"
"port.ss"
file/gzip
file/gunzip
scheme/port
)
(define (make-input-encrypted-pipe cipher key iv)
(lambda (port)
(decrypt cipher key iv)))
(define (make-output-encrypted-pipe cipher key iv)
(lambda (port)
(encrypt cipher key iv)))
(define (open-input-encrypted-port in cipher key iv)
(make-input-filter-port in #f (make-input-encrypted-pipe cipher key iv)))
(define call-with-input-encrypted-port
(make-call-with-input-port open-input-encrypted-port in cipher key iv))
(define (open-input-encrypted-file path cipher key iv)
(open-input-encrypted-port (open-input-file path) cipher key iv))
(define call-with-input-encrypted-file
(make-call-with-input-port open-input-encrypted-file path cipher key iv))
(define (open-output-encrypted-port out cipher key iv)
(make-output-filter-port out #f (make-output-encrypted-pipe cipher key iv)))
(define call-with-output-encrypted-port
(make-call-with-output-port open-output-encrypted-port out cipher key iv))
(define (open-output-encrypted-file path cipher key iv #:exists (exists 'replace))
(open-output-encrypted-port (open-output-file path #:exists exists) cipher key iv))
(define call-with-output-encrypted-file
(make-call-with-output-port open-output-encrypted-file path cipher key iv #:exists (exists 'replace)))
(provide/contract
(make-input-encrypted-pipe (-> cipher? bytes? bytes? make-pipe/c))
(make-output-encrypted-pipe (-> cipher? bytes? bytes? make-pipe/c))
(open-input-encrypted-port (-> input-port? cipher? bytes? bytes? input-port?))
(call-with-input-encrypted-port (-> input-port? cipher? bytes? bytes? (-> input-port? any) any))
(open-input-encrypted-file (-> path-string? cipher? bytes? bytes? input-port?))
(call-with-input-encrypted-file (-> path-string? cipher? bytes? bytes? (-> input-port? any) any))
(open-output-encrypted-port (-> output-port? cipher? bytes? bytes? output-port?))
(call-with-output-encrypted-port (-> output-port? cipher? bytes? bytes? (-> output-port? any) any))
(open-output-encrypted-file (->* (path-string? cipher? bytes? bytes?)
(#:exists (or/c 'replace 'error))
output-port?))
(call-with-output-encrypted-file (->* (path-string? cipher? bytes? bytes? (-> output-port? any))
(#:exists (or/c 'replace 'error))
any))
)