summaryrefslogtreecommitdiff
path: root/type7
blob: e92cb661ae5508291fe8f5d2282a3f2489fafe45 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby

module CiscoType7
  MAGIC="dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"

  def CiscoType7.encode(pass, salt = (0..15).to_a.sample)
    out = "%02d" % salt
    (0...pass.length).each do |i|
      j = (i + salt) % 53
      byte = pass[i].ord ^ MAGIC[j].ord
      out += "%02X" % byte
    end
    return out
  end

  def CiscoType7.decode(hash)
    out = ""
    salt = hash[0..1].to_i
    pass = hash[2..-1]
    (0...(pass.length/2)).each do |i|
      j = (i + salt) % 53
      byte = pass[(i*2)..((i*2)+1)].to_i(16) ^ MAGIC[j].ord
      out += byte.chr
    end
    return out
  end

  def CiscoType7.test_equal(hash1, hash2)
    return decode(hash1) == decode(hash2)
  end
end

def mode_looks_like(args)
  return :equal if args.length == 2
  return :decode if args[0] =~ /^(0[0-9]|1[0-5])([0-9A-F]{2})+$/
  return :encode
end

name = {
  equal: "Test Equality",
  decode: "Decode",
  encode: "Encode"
}

require 'optparse'

options = {:mode => :magic}

OptionParser.new do |opts|
  opts.banner = "Usage: type7 [options] ARG1 [ARG2]"

  opts.on("-e", "--encode", "Encode mode") do
    options[:mode] = :encode
  end

  opts.on("-d", "--decode", "Decode mode") do
    options[:mode] = :decode
  end

  opts.on("-q", "--equal", "Test equality mode") do
    options[:mode] = :equal
  end

  opts.on("-h", "--help", "Display help") do
    puts opts.banner
    puts opts.summarize
    puts "The default mode is magic mode, where type7 will work out what you want to do."
    exit 0
  end
end.parse!

if options[:mode] == :magic
  print "Magic mode casts: "
  options[:mode] = mode_looks_like(ARGV)
  puts name[options[:mode]]
end

case options[:mode]
when :encode
  puts CiscoType7::encode(ARGV[0])
when :decode
  puts CiscoType7::decode(ARGV[0])
when :equal
  puts CiscoType7::test_equal(ARGV[0], ARGV[1]) ? "Match" : "Differ"
end