Module: Netpayclient

Defined in:
lib/netpayclient.rb,
lib/netpayclient/version.rb

Defined Under Namespace

Modules: Crypto Classes: Netpayclient

Constant Summary collapse

DES_KEY =
'SCUBEPGW'
HASH_PAD =
'0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003021300906052b0e03021a05000414'
VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_key(path: nil, hash: {}) ⇒ Object


30
31
32
# File 'lib/netpayclient.rb', line 30

def self.build_key(path: nil, hash: {})
  Netpayclient.new(path: path, hash: hash)
end

Instance Method Details

#bcdechex(decdata) ⇒ Object


56
57
58
# File 'lib/netpayclient.rb', line 56

def bcdechex(decdata)
  decdata.to_s(16)
end

#bchexdec(hexdata) ⇒ Object


52
53
54
# File 'lib/netpayclient.rb', line 52

def bchexdec(hexdata)
  hexdata.to_i(16)
end

#bin2int(bindata) ⇒ Object


48
49
50
# File 'lib/netpayclient.rb', line 48

def bin2int(bindata)
  bchexdec(bindata.unpack('H*')[0])
end

#hex2bin(hexdata) ⇒ Object


34
35
36
# File 'lib/netpayclient.rb', line 34

def hex2bin(hexdata)
  [hexdata].pack "H*"
end

#mybcpowmod(num, pow, mod) ⇒ Object


68
69
70
# File 'lib/netpayclient.rb', line 68

def mybcpowmod(num, pow, mod)
  num.to_bn.mod_exp(pow, mod)
end

#padstr(src, len = 256, chr = '0', d = 'L') ⇒ Object


38
39
40
41
42
43
44
45
46
# File 'lib/netpayclient.rb', line 38

def padstr(src, len=256, chr='0', d='L')
  src.strip!
  case d
  when 'L'
    src.rjust(len, chr)
  else
    src.ljust(len, chr)
  end
end

#rsa_encrypt(private_key, input) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/netpayclient.rb', line 72

def rsa_encrypt(private_key, input)
  p = bin2int(private_key[:prime1])
  q = bin2int(private_key[:prime2])
  u = bin2int(private_key[:coefficient])
  dP = bin2int(private_key[:prime_exponent1])
  dQ = bin2int(private_key[:prime_exponent2])
  c = bin2int(input)
  cp = c % p
  cq = c % q
  a = mybcpowmod(cp, dP, p)
  b = mybcpowmod(cq, dQ, q)
  if a > b
    result = a - b
  else
    result = b - a
    result = p - result
  end
  result = result % p
  result = result * u
  result = result % p
  result = result * q
  result = result + b
  ret = bcdechex(result)
  ret = padstr(ret).upcase
  ret.size == 256 ? ret : false
end

#sha1_128(string) ⇒ Object


60
61
62
63
64
65
66
# File 'lib/netpayclient.rb', line 60

def sha1_128(string)
  require 'digest/sha1'
  hash = Digest::SHA1.hexdigest(string)
  sha_bin = hex2bin(hash)
  sha_pad = hex2bin(HASH_PAD)
  sha_pad + sha_bin
end