mk4vfs encryption

This is a small example on how to use mk4vfs and actually encrypt the corresponding data on the HDD. This can also be used as an example how to encrypt a metakit database as well. This example requires Trfcrypt, which can now be compiled inside DQkit, but is not due to export regulations.

 proc doattach {fh} {
    package require Trfcrypt
    fconfigure $fh -translation binary
    blowfish -attach $fh -key "SomeMK4CryptVFSKey01" -mode cfb -shift 1 -iv "0th3rk3Y" -direction encrypt
 }
 
 proc readEncryptedFS {db file} {
    set fh [open $file r]
    doattach $fh
    mk::file load $db $fh
    close $fh
 }
 
 proc writeEncryptedFS {db file} {
    set fh [open $file.tmp[pid] w]
    doattach $fh
    mk::file save $db $fh
    close $fh
    catch {file delete $file}
    catch {file rename $file.tmp[pid] $file}
 }
 
 set local [info script]
 
 set db [vfs::mk4::Mount "" $local]
 
 switch -- [lindex $argv 0] {
    read {
         set t [time {
             readEncryptedFS $db mk.raw
             for {set i 0} {$i < 1000} {incr i} {
                 set fh [open $local/file$i r]
                 set fc [read $fh]
                 close $fh
                 if {![string is integer $fc]} {
                     error "$fc is not an integer"
                 }
             }
         }]
         puts "Read in $t"    
    }
    write {
         set t [time {
             for {set i 0} {$i < 1000} {incr i} {
                 set fh [open $local/file$i w]
                 puts -nonewline $fh [clock seconds]
                 close $fh
             }
             writeEncryptedFS $db mk.raw
         }]
         puts "Written in $t"
    }
    read {
    }
    default {
         puts "Usage: [info script] read|write"
    }
 }