<filename>src/python/detectors/hashlib_constructor/hashlib_constructor.py
#  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#  SPDX-License-Identifier: Apache-2.0

# {fact rule=hashlib-constructor@v1.0 defects=1}
def constructor_noncompliant():
    import hashlib

    text = "ExampleString"

    # Noncompliant: uses the new() constructor instead of the hashlib
    # constructor, which is slower.
    result = hashlib.new('sha256', text.encode())

    print("The hexadecimal equivalent of SHA256 is : ")
    print(result.hexdigest())
# {/fact}


# {fact rule=hashlib-constructor@v1.0 defects=0}
def constructor_compliant():
    import hashlib

    text = "ExampleString"

    # Compliant: uses the hashlib constructor over the new(), which is faster.
    result = hashlib.sha256(text.encode())

    print("The hexadecimal equivalent of SHA256 is : ")
    print(result.hexdigest())
# {/fact}

<filename>src/python/detectors/hashlib_constructor/hashlib_constructor.py
#  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#  SPDX-License-Identifier: Apache-2.0

# {fact rule=hashlib-constructor@v1.0 defects=1}
def constructor_noncompliant():
    import hashlib

    text = "ExampleString"

    # Noncompliant: uses the new() constructor instead of the hashlib
    # constructor, which is slower.
    result = hashlib.new('sha256', text.encode())

    print("The hexadecimal equivalent of SHA256 is : ")
    print(result.hexdigest())
# {/fact}


# {fact rule=hashlib-constructor@v1.0 defects=0}
def constructor_compliant():
    import hashlib

    text = "ExampleString"

    # Compliant: uses the hashlib constructor over the new(), which is faster.
    result = hashlib.sha256(text.encode())

    print("The hexadecimal equivalent of SHA256 is : ")
    print(result.hexdigest())
# {/fact}

#  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #  SPDX-License-Identifier: Apache-2.0 # {fact rule=hashlib-constructor@v1.0 defects=1} # Noncompliant: uses the new() constructor instead of the hashlib # constructor, which is slower. # {/fact} # {fact rule=hashlib-constructor@v1.0 defects=0} # Compliant: uses the hashlib constructor over the new(), which is faster. # {/fact}