From 5f821e510f14f7a38814183e566f52a7d3c174e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Thu, 21 Jan 2021 00:07:23 +0100 Subject: [PATCH] Module4 - has been started MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- module.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/module.rb b/module.rb index 657cae3..9d88025 100755 --- a/module.rb +++ b/module.rb @@ -564,3 +564,71 @@ def algorytmDeSzyfrowania(x,y,u) m = (x - 1) / u return m end + +# Module 4 +################################################################################# +# Zadanie 1 +# Funkcja suma(a,b) wykorzystujac liczby hex +################################################################################# +def suma(a,b) + binA = a.to_i(16).to_s(2) + binB = b.to_i(16).to_s(2) + + if (a =~ /[^01]/).nil? && (b =~ /[^01]/).nil? + return (a.to_i(2) ^ b.to_i(2)).to_s(2) + else + return (binA.to_i(2) ^ binB.to_i(2)).to_s(16) + end +end + +################################################################################# +# Zadanie 2 +# Funkcja xtime(a) wykorzystujac liczby hex +################################################################################# +def xtime(a) + binA = a.to_i(16).to_s(2) + dl = binA.length + while dl != 8 + binA = "0" + binA + dl = dl + 1 + end + if binA[0].to_i == 1 + binA[0] = '' + binA = binA + "0" + return (binA.to_i(2) ^ "1B".to_i(16).to_s(2).to_i(2)).to_s(16) + elsif binA[0].to_i == 0 + binA[0] = '' + return (binA.to_i(2) << 1 ).to_s(16) + end +end + +################################################################################# +# Zadanie 3 +# Funkcja iloczyn(a,b) wykorzystujac liczby hex +################################################################################# +def iloczyn(a,b) + solve = "" + + binA = a.to_i(16).to_s(2) + dl = binA.length + while dl != 8 + binA = "0" + binA + dl = dl + 1 + end + + len = binA.length - 1 + binA.split('').each { |a| + counter = len - 2 + tmp = xtime(b) + puts counter + while counter != 0 + tmp = xtime(tmp) + counter = counter - 1 + end + puts tmp + #puts solve + solve = suma((a.to_i * tmp.to_i(16)).to_s(16), solve) + len = len - 1 + } + return solve +end