cryptmt/cmd/main.go

84 lines
1.7 KiB
Go

// WANDERER - a crypto doodle that appears to give adequate
// protection to data in a stream cipher context (?)
//
// Properties visualized using https://github.com/circulosmeos/circle
//
// test command-line 'main' program
package main
// TODOs:
// -use a crypto rand (eg mtwist64) instead of go pkg rand?
// -define s-box rotation/shuffle schema
// -devise p-box schema
// ...
import (
"flag"
"io"
"log"
"os"
"blitter.com/go/cryptmt"
)
const (
invalidKey = "FATAL_UNSPECIFIED_KEY"
minKeyLen = 8
)
var (
k string
)
/* Wrap the cipher in a type which implements the
* Reader and Writer interfaces to enable its use via
* io.Copy(). As evident below, DO NOT rely on
* Write() by itself to do anything other than output
* what should have been encrypted or decrypted by
* Read()!
*
* (That is, calling Write() directly, as should be
* obvious below,does nothing to do with crypto.)
*
* To use the cipher outside of io.Copy(), use
* the crypto/cipher.StreamReader/StreamWriter interfaces
* which will make use of the cipher's XORKeyStream method.
*/
type Encoder struct {
r io.Reader
w io.Writer
c *cryptmt.Cipher
}
func (c *Encoder) Read(p []byte) (n int, err error) {
n, err = c.r.Read(p)
if err == nil {
for idx := 0; idx < n; idx++ {
p[idx] = c.c.Yield(p[idx])
}
}
return n, err
}
func (c *Encoder) Write(p []byte) (n int, err error) {
n, err = c.w.Write(p)
return n, err
}
func main() {
flag.StringVar(&k, "k", "WARNING_DEFAULT_KEY", "Key (NOTE insecure specified on command line)")
flag.Parse()
if k == invalidKey {
log.Fatal("no key specified")
}
if len(k) < minKeyLen {
log.Fatal("key is too small")
}
c := &Encoder{}
c.r = os.Stdin
c.w = os.Stdout
c.c = cryptmt.New([]byte(k))
_, _ = io.Copy(c, c)
}