-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone-time_pad.sf
51 lines (36 loc) · 1.04 KB
/
one-time_pad.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 20 Novermber 2016
# https://github.com/trizen
# One-time pad symmetric encryption, where the key is pseudo-randomly generated from a given seed.
# See also:
# https://en.wikipedia.org/wiki/One-time_pad
define READ_SIZE = (2 * 1024**2) # 2 MB
func usage(err=nil) {
if (defined(err)) {
STDERR.say("\n[ERROR]: #{err}\n")
}
var name = File(__MAIN__).base
print <<"USAGE"
usage: #{name} [seed] [<input] [>output]
example:
#{name} 42 < original.txt > encrypted.dat
#{name} 42 < encrypted.dat > decrypted.txt
USAGE
Sys.exit(1)
}
func encrypt(in_fh, out_fh, seed) {
in_fh.binmode(':raw')
out_fh.binmode(':raw')
seed.iseed
loop {
var len = in_fh.read(\var chunk, READ_SIZE)
var key = len.of { 255.irand }.pack('C*')
out_fh.print(chunk ^ key)
len == READ_SIZE || break
}
return true
}
var seed = Num(ARGV[0] \\ usage("No seed has been specified!"))
encrypt(in_fh: STDIN, out_fh: STDOUT, seed: seed)