Module: Samba::Encrypt::Private

Defined in:
lib/samba/encrypt.rb

Constant Summary collapse

LM_MAGIC =
"KGS!@\#$%"

Class Method Summary collapse

Class Method Details

.convert_encoding(to, from, str) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/samba/encrypt.rb', line 32

def convert_encoding(to, from, str)
  if same_encoding?(to, from)
    str
  else
    require 'iconv'
    Iconv.iconv(to, from, str).join
  end
end

.des_crypt56(input, key_str, forward_only) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/samba/encrypt.rb', line 69

def des_crypt56(input, key_str, forward_only)
  key = str_to_key(key_str)
  encoder = OpenSSL::Cipher::DES.new
  encoder.encrypt
  encoder.key = key
  encoder.update(input)
end

.encrypt_14characters(chars) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
# File 'lib/samba/encrypt.rb', line 78

def encrypt_14characters(chars)
  raise ArgumentError.new("must be <= 14 characters") if chars.size > 14
  chars = chars.to_s.ljust(14, "\000")
  des_crypt56(LM_MAGIC, chars[0, 7], true) +
    des_crypt56(LM_MAGIC, chars[7, 7], true)
end

.normalize_encoding(encoding) ⇒ Object



41
42
43
# File 'lib/samba/encrypt.rb', line 41

def normalize_encoding(encoding)
  encoding.downcase.gsub(/-/, "_")
end

.same_encoding?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/samba/encrypt.rb', line 45

def same_encoding?(a, b)
  na = normalize_encoding(a)
  nb = normalize_encoding(b)
  na == nb or na.gsub(/_/, '') == nb.gsub(/_/, '')
end

.str_to_key(str) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/samba/encrypt.rb', line 51

def str_to_key(str)
  key = "\000" * 8
  key[0] = str[0] >> 1;
  key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
  key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
  key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
  key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
  key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
  key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
  key[7] = str[6] & 0x7F;

  key.size.times do |i|
    key[i] = (key[i] << 1);
  end

  key
end