diff options
| author | william <william@25thandclement.com> | 2015-02-25 14:58:33 -0800 |
|---|---|---|
| committer | william <william@25thandclement.com> | 2015-02-25 14:58:33 -0800 |
| commit | 7dce9c391259f1b72f949f73ac1604d6fcb79fdd (patch) | |
| tree | 5f60be305aab04077da5fbcd8e3b4ee0e46d9ffd /examples | |
| parent | 1476267d26c9b0cac8818bf7a74696d48505b79d (diff) | |
| download | luaossl-7dce9c391259f1b72f949f73ac1604d6fcb79fdd.tar.gz luaossl-7dce9c391259f1b72f949f73ac1604d6fcb79fdd.tar.bz2 luaossl-7dce9c391259f1b72f949f73ac1604d6fcb79fdd.zip | |
document des.set_odd_parity and add example
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/lm.hash | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/lm.hash b/examples/lm.hash new file mode 100755 index 0000000..4d8157c --- /dev/null +++ b/examples/lm.hash | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | _=[[ | ||
| 3 | : ${LUA:=$(command -v lua-5.2)} | ||
| 4 | : ${LUA:=$(command -v lua5.2)} | ||
| 5 | : ${LUA:=$(command -v lua-52)} | ||
| 6 | : ${LUA:=$(command -v lua52)} | ||
| 7 | : ${LUA:=$(command -v luajit)} | ||
| 8 | : ${LUA:=$(command -v lua)} | ||
| 9 | |||
| 10 | exec ${LUA} "$0" "$@" | ||
| 11 | ]] | ||
| 12 | |||
| 13 | local des = require"openssl.des" | ||
| 14 | local cipher = require"openssl.cipher" | ||
| 15 | local bit32 = require"bit32" | ||
| 16 | |||
| 17 | local function lm_encrypt(key) | ||
| 18 | return cipher.new"DES-ECB":encrypt(key, nil, false):final"KGS!@#$%" | ||
| 19 | end -- lm_encrypt | ||
| 20 | |||
| 21 | local lshift = bit32.lshift | ||
| 22 | local band = bit32.band | ||
| 23 | local rshift = bit32.rshift | ||
| 24 | local bor = bit32.bor | ||
| 25 | |||
| 26 | local function lm_string_to_key(s) | ||
| 27 | local s0, s1, s2, s3, s4, s5, s6 = string.byte(s, 1, 7) | ||
| 28 | local k0, k1, k2, k3, k4, k5, k6, k7 | ||
| 29 | |||
| 30 | s0 = s0 or 0 | ||
| 31 | s1 = s1 or 0 | ||
| 32 | s2 = s2 or 0 | ||
| 33 | s3 = s3 or 0 | ||
| 34 | s4 = s4 or 0 | ||
| 35 | s5 = s5 or 0 | ||
| 36 | s6 = s6 or 0 | ||
| 37 | |||
| 38 | k0 = s0 | ||
| 39 | k1 = bor(band(lshift(s0, 7), 255), rshift(s1, 1)) | ||
| 40 | k2 = bor(band(lshift(s1, 6), 255), rshift(s2, 2)) | ||
| 41 | k3 = bor(band(lshift(s2, 5), 255), rshift(s3, 3)) | ||
| 42 | k4 = bor(band(lshift(s3, 4), 255), rshift(s4, 4)) | ||
| 43 | k5 = bor(band(lshift(s4, 3), 255), rshift(s5, 5)) | ||
| 44 | k6 = bor(band(lshift(s5, 2), 255), rshift(s6, 6)) | ||
| 45 | k7 = band(lshift(s6, 1), 255) | ||
| 46 | |||
| 47 | return des.set_odd_parity(string.char(k0, k1, k2, k3, k4, k5, k6, k7)) | ||
| 48 | end -- lm_string_to_key | ||
| 49 | |||
| 50 | local function lm_hash(pass) | ||
| 51 | pass = string.upper(pass) | ||
| 52 | |||
| 53 | if #pass < 14 then | ||
| 54 | pass = pass .. string.rep(string.char(0), 14 - #pass) | ||
| 55 | end | ||
| 56 | |||
| 57 | local key1 = lm_string_to_key(string.sub(pass, 1, 7)) | ||
| 58 | local key2 = lm_string_to_key(string.sub(pass, 8, 14)) | ||
| 59 | |||
| 60 | return lm_encrypt(key1) .. lm_encrypt(key2) | ||
| 61 | end -- lm_hash | ||
| 62 | |||
| 63 | local function tohex(s) | ||
| 64 | return (string.gsub(s, ".", function (c) | ||
| 65 | return string.format("%.2x", string.byte(c)) | ||
| 66 | end)) | ||
| 67 | end -- tohex | ||
| 68 | |||
| 69 | local pass = ... or "passphrase" | ||
| 70 | |||
| 71 | print(pass, tohex(lm_hash(... or "passphrase"))) | ||
