Module4 - has been started

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-21 00:07:23 +01:00
parent 6b39a7a071
commit 5f821e510f
Signed by: y0rune
GPG Key ID: F204C385F57EB348
1 changed files with 68 additions and 0 deletions

View File

@ -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