Mask generation function: Difference between revisions

Content deleted Content added
top: see the talk page
m Reverted edits by 73.167.116.198 (talk) (HG) (3.4.12)
 
(5 intermediate revisions by 3 users not shown)
Line 1:
{{Short description|Cryptographic tool}}
{{Use American English|date = April 2019}}
A '''mask generation function''' ('''MGF''') is a cryptographic primitive similar to a [[cryptographic hash function]] except that while a hash function's output has a fixed size, a MGF supports output of a variable length. In this respect, a MGF can be viewed as a [[extendedextendable-output function]] (XOF) function: it can accept input of any length and process it to produce output of any length. Mask generation functions are completely deterministic: for any given input and any desired output length the output is always the same.
 
== Definition ==
Line 69:
hLen = hash_func().digest_size
# https://www.ietf.org/rfc/rfc2437.txt
# 1. If l > 2^32(hLen), output "mask too long" and stop.
if length > (hLen << 32):
raise ValueError("mask too long")
# 2. Let T be the empty octet string.
T = b""
# 3. For counter from 0 to \lceil{l / hLen}\rceil-1, do the following:
# Note: \lceil{l / hLen}\rceil-1 is the number of iterations needed,
# but it's easier to check if we have reached the desired length.
counter = 0
while len(T) < length:
# a. Convert counter to an octet string C of length 4 with the primitive I2OSP: C = I2OSP (counter, 4)
C = int.to_bytes(counter, 4, '"big'")
# b. Concatenate the hash of the seed Z and C to the octet string T: T = T || Hash (Z || C)
T += hash_func(seed + C).digest()
counter += 1
# 4. Output the leading l octets of T as the octet string mask.
return T[:length]
</syntaxhighlight>
Line 110:
{{reflist}}
 
[[Category:Articles with example Python (programming language) code]]
[[Category:Cryptographic primitives]]
[[Category:Cryptographic hash functions]]