Mask generation function: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Alter: template type. Add: doi, year. | Use this bot. Report bugs. | Suggested by Abductive | Category:Use American English from April 2019 | #UCB_Category 495/940
m Reverted edits by 73.167.116.198 (talk) (HG) (3.4.12)
 
(8 intermediate revisions by 4 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 single[[extendable-use XORoutput function]] (XOF): 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 60:
=== Example code ===
 
Below is Python3Python code implementing MGF1:
 
<syntaxhighlight lang="python">
Line 66:
 
def mgf1(seed: bytes, length: int, hash_func=hashlib.sha1) -> bytes:
"""Mask generation function."""
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 109 ⟶ 110:
{{reflist}}
 
[[Category:Articles with example Python (programming language) code]]
[[Category:Cryptography]]
[[Category:Cryptographic primitives]]
[[Category:Cryptographic hash functions]]