class CryptoToolchain::Utilities::SHA1
Constants
- CONSTANTS
- F_FUNCTIONS
- INITIAL_STATE
Equivalent to [ 0x67452301, 0xefcdaB89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ] when using registers
- K_CONSTANTS
Attributes
original[R]
Public Class Methods
bindigest(str, state: INITIAL_STATE, append_length: 0)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 10 def bindigest(str, state: INITIAL_STATE, append_length: 0) CryptoToolchain::Utilities::SHA1.new(str).bindigest(state: state, append_length: append_length) end
Also aliased as: digest
hexdigest(str, state: INITIAL_STATE, append_length: 0 )
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 6 def hexdigest(str, state: INITIAL_STATE, append_length: 0 ) CryptoToolchain::Utilities::SHA1.new(str).hexdigest(state: state, append_length: append_length) end
new(message)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 21 def initialize(message) @original = message end
padding(str)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 15 def padding(str) num_null_pad = (56 - (str.bytesize + 1) ) % 64 0x80.chr + (0.chr * num_null_pad) + [str.bytesize * 8].pack("Q>") end
Public Instance Methods
bindigest(state: INITIAL_STATE, append_length: 0)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 29 def bindigest(state: INITIAL_STATE, append_length: 0) h = registers_from(state).dup length = original.bytesize + append_length # while (string.size % 64) != 56 num_null_pad = (56 - (length + 1) ) % 64 padding = 0x80.chr + (0.chr * num_null_pad) + [length * 8].pack("Q>") (original + padding).in_blocks(64).each do |_block| w = _block.unpack("L>16") (16..79).each do |i| w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]).lrot(1) end a, b, c, d, e = h (0..79).each do |i| func, k = f_and_k_for(i) f = func.call(b, c, d) temp = (a.lrot(5) + f + e + k + w[i]) & 0xffffffff e = d d = c c = b.lrot(30) b = a a = temp end [a, b, c, d, e].each_with_index do |val, i| h[i] = (h[i] + val) & 0xffffffff end end h.pack("L>5") end
Also aliased as: digest
hexdigest(state: INITIAL_STATE, append_length: 0)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 25 def hexdigest(state: INITIAL_STATE, append_length: 0) bindigest(state: state, append_length: append_length).unpack("H*").join end
Private Instance Methods
f_and_k_for(i)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 89 def f_and_k_for(i) raise ArgumentError.new("i must be in 0..79") unless i >=0 && i <= 79 CONSTANTS[i/20] end
registers_from(hex_str)
click to toggle source
# File lib/crypto_toolchain/utilities/sha1.rb, line 83 def registers_from(hex_str) raise ArgumentError.new("Argument must be a hex string") unless hex_str.hex? raise ArgumentError.new("Argument must be 40 characters long") unless hex_str.length == 40 hex_str.from_hex.unpack("L>*") end