Skip to content

Commit

Permalink
Set usedforsecurity=False in hashlib methods (FIPS compliance) (hug…
Browse files Browse the repository at this point in the history
…gingface#5790)

* Set usedforsecurity=False in hashlib methods (FIPS compliance)

* update version dependency

* bump hfh version

* bump hfh version
  • Loading branch information
Wauplin authored and Jimmy committed Apr 26, 2024
1 parent a26bc1b commit c8ad210
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions examples/custom_diffusion/train_custom_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and

import argparse
import hashlib
import itertools
import json
import logging
Expand All @@ -35,6 +34,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import HfApi, create_repo
from huggingface_hub.utils import insecure_hashlib
from packaging import version
from PIL import Image
from torch.utils.data import Dataset
Expand Down Expand Up @@ -760,7 +760,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = (
class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
)
Expand Down
4 changes: 2 additions & 2 deletions examples/dreambooth/train_dreambooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import argparse
import copy
import gc
import hashlib
import importlib
import itertools
import logging
Expand All @@ -35,6 +34,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import create_repo, model_info, upload_folder
from huggingface_hub.utils import insecure_hashlib
from packaging import version
from PIL import Image
from PIL.ImageOps import exif_transpose
Expand Down Expand Up @@ -877,7 +877,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
4 changes: 2 additions & 2 deletions examples/dreambooth/train_dreambooth_flax.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import hashlib
import logging
import math
import os
Expand All @@ -16,6 +15,7 @@
from flax.training import train_state
from flax.training.common_utils import shard
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from jax.experimental.compilation_cache import compilation_cache as cc
from PIL import Image
from torch.utils.data import Dataset
Expand Down Expand Up @@ -373,7 +373,7 @@ def main():
images = pipeline.numpy_to_pil(np.array(images))

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
4 changes: 2 additions & 2 deletions examples/dreambooth/train_dreambooth_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import argparse
import copy
import gc
import hashlib
import itertools
import logging
import math
Expand All @@ -34,6 +33,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from packaging import version
from PIL import Image
from PIL.ImageOps import exif_transpose
Expand Down Expand Up @@ -735,7 +735,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
4 changes: 2 additions & 2 deletions examples/dreambooth/train_dreambooth_lora_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import argparse
import gc
import hashlib
import itertools
import logging
import math
Expand All @@ -33,6 +32,7 @@
from accelerate.logging import get_logger
from accelerate.utils import DistributedDataParallelKwargs, ProjectConfiguration, set_seed
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from packaging import version
from PIL import Image
from PIL.ImageOps import exif_transpose
Expand Down Expand Up @@ -648,7 +648,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import hashlib
import math
import os
from pathlib import Path
Expand All @@ -16,6 +15,7 @@
from colossalai.utils import get_current_device
from colossalai.utils.model.colo_init_context import ColoInitContext
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
Expand Down Expand Up @@ -394,7 +394,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import hashlib
import itertools
import math
import os
Expand All @@ -14,6 +13,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from PIL import Image, ImageDraw
from torch.utils.data import Dataset
from torchvision import transforms
Expand Down Expand Up @@ -465,7 +465,7 @@ def main():
images = pipeline(prompt=example["prompt"], mask_image=fake_mask, image=fake_pil_images).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import hashlib
import math
import os
import random
Expand All @@ -13,6 +12,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from PIL import Image, ImageDraw
from torch.utils.data import Dataset
from torchvision import transforms
Expand Down Expand Up @@ -464,7 +464,7 @@ def main():
images = pipeline(prompt=example["prompt"], mask_image=fake_mask, image=fake_pil_images).images

for i, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg"
image.save(image_filename)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import hashlib
import itertools
import json
import logging
Expand All @@ -21,6 +20,7 @@
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
from huggingface_hub import create_repo, upload_folder
from huggingface_hub.utils import insecure_hashlib
from PIL import Image
from torch import dtype
from torch.nn import Module
Expand Down Expand Up @@ -843,7 +843,7 @@ def main(args):
images = pipeline(example["prompt"]).images

for ii, image in enumerate(images):
hash_image = hashlib.sha1(image.tobytes()).hexdigest()
hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest()
image_filename = (
class_images_dir / f"{example['index'][ii] + cur_class_images}-{hash_image}.jpg"
)
Expand Down
6 changes: 3 additions & 3 deletions scripts/convert_consistency_decoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
import math
import os
import urllib
Expand All @@ -8,6 +7,7 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
from huggingface_hub.utils import insecure_hashlib
from safetensors.torch import load_file as stl
from tqdm import tqdm

Expand Down Expand Up @@ -51,7 +51,7 @@ def _download(url: str, root: str):
raise RuntimeError(f"{download_target} exists and is not a regular file")

if os.path.isfile(download_target):
if hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256:
if insecure_hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256:
return download_target
else:
warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file")
Expand All @@ -72,7 +72,7 @@ def _download(url: str, root: str):
output.write(buffer)
loop.update(len(buffer))

if hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256:
if insecure_hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256:
raise RuntimeError("Model has been downloaded but the SHA256 checksum does not not match")

return download_target
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"filelock",
"flax>=0.4.1",
"hf-doc-builder>=0.3.0",
"huggingface-hub>=0.13.2",
"huggingface-hub>=0.19.4",
"requests-mock==1.10.0",
"importlib_metadata",
"invisible-watermark>=0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/dependency_versions_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"filelock": "filelock",
"flax": "flax>=0.4.1",
"hf-doc-builder": "hf-doc-builder>=0.3.0",
"huggingface-hub": "huggingface-hub>=0.13.2",
"huggingface-hub": "huggingface-hub>=0.19.4",
"requests-mock": "requests-mock==1.10.0",
"importlib_metadata": "importlib_metadata",
"invisible-watermark": "invisible-watermark>=0.2.0",
Expand Down

0 comments on commit c8ad210

Please sign in to comment.