summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2011-08-05 23:38:26 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2011-08-05 23:38:26 +0200
commitbfd398596afed66754011b6720e5891c4897c858 (patch)
treec82b2c7f107a0eb08b3a31da24f2179759aa105a /lib
parent91d70d775dd11a9ec350ca7f3b6d403fa897aadc (diff)
downloadayk-bfd398596afed66754011b6720e5891c4897c858.zip
ayk-bfd398596afed66754011b6720e5891c4897c858.tar.gz
update jcryption and specs
Diffstat (limited to 'lib')
-rw-r--r--lib/ayk/jcryption.rb75
1 files changed, 4 insertions, 71 deletions
diff --git a/lib/ayk/jcryption.rb b/lib/ayk/jcryption.rb
index 6e4d1ed..3b4871f 100644
--- a/lib/ayk/jcryption.rb
+++ b/lib/ayk/jcryption.rb
@@ -1,14 +1,7 @@
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
-
-#----------------------------------------------------------------------------
#
-# File : jcryption.rb
-# Author : Jérémy Zurcher <jeremy@asynk.ch>
-# Date : 11/09/09
-# License :
-#
-# Copyright (c) 2009 Jérémy Zurcher
+# Copyright (c) 2009-2011 Jérémy Zurcher
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@@ -31,75 +24,15 @@
#
#----------------------------------------------------------------------------
#
-def Math.power_modulo(b, p, m)
- if p == 1
- b % m
- elsif (p & 0x1) == 0
- t = power_modulo(b, p >> 1, m)
- (t * t) % m
- else
- (b * power_modulo(b, p-1, m)) % m
- end
-end
-#
-class Fixnum
- def to_bytes
- str = ''
- int = self
- while int!=0 do
- int,r = int.divmod 256
- str += r.chr
- end
- str
- end
-end
-#
-class Bignum
- #
- class << self
- def from_bytes bytes
- val = 0
- idx = 0
- bytes.each_byte{ |b|
- val += b << 8 * idx
- idx += 1
- }
- val
- end
- end
- #
- def to_bytes
- str = ''
- bint = self
- while bint!=0 do
- bint,r = bint.divmod 256
- str += r.chr
- end
- str
- end
- #
- def bits
- s = self.to_bytes
- while s[-1].ord==0
- s.slice!(-1)
- end
- bit_len = s.length * 8
- tmp = s[-1].ord
- while not tmp & 0x80
- bit_len-=1
- tmp <<=1
- end
- bit_len
- end
-end
+require 'ayk/maths'
#
module JCryption
#
def decrypt data, d, n
- s = data.split(' ').inject('') do |str,block| str += ( Math::power_modulo( block.to_i(16), d, n ) ).to_bytes end
+ s = data.split(' ').inject('') do |str,block| str += ( Math::prime_power_modulus( block.to_i(16), d, n ) ).to_bytes end
sum,crc = 0,s.slice(0,2).to_i(16)
s.slice(2..-1).each_char do |char| sum += char.ord end
- return ( crc == sum & 0xFF ) ? s : nil
+ return ( crc == sum & 0xFF ) ? s : nil
end
module_function :decrypt
#