"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.0/zun/common/docker_image/digest.py" (16 Oct 2019, 1587 Bytes) of package /linux/misc/openstack/zun-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "digest.py" see the Fossies "Dox" file reference documentation.

    1 # Licensed under the Apache License, Version 2.0 (the "License");
    2 # you may not use this file except in compliance with the License.
    3 # You may obtain a copy of the License at
    4 #
    5 #    http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS,
    9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   10 # implied.
   11 # See the License for the specific language governing permissions and
   12 # limitations under the License.
   13 
   14 from zun.common.docker_image import regexp
   15 
   16 DigestRegexps = regexp.DigestRegexps
   17 
   18 
   19 class InvalidDigest(Exception):
   20     @classmethod
   21     def default(cls):
   22         return cls("invalid digest")
   23 
   24 
   25 class DigestUnsupported(InvalidDigest):
   26     @classmethod
   27     def default(cls):
   28         return cls("unsupported digest algorithm")
   29 
   30 
   31 class DigestInvalidLength(InvalidDigest):
   32     @classmethod
   33     def default(cls):
   34         return cls("invalid checksum digest length")
   35 
   36 
   37 DIGESTS_SIZE = {
   38     'sha256': 32,
   39     'sha384': 48,
   40     'sha512': 64,
   41 }
   42 
   43 
   44 def validate_digest(digest):
   45     matched = DigestRegexps.DIGEST_REGEXP_ANCHORED.match(digest)
   46     if not matched:
   47         raise InvalidDigest.default()
   48 
   49     i = digest.find(':')
   50     # case: "sha256:" with no hex.
   51     if i < 0 or ((i + 1) == len(digest)):
   52         raise InvalidDigest.default()
   53 
   54     algorithm = digest[:i]
   55     if algorithm not in DIGESTS_SIZE:
   56         raise DigestUnsupported.default()
   57 
   58     if DIGESTS_SIZE[algorithm] * 2 != len(digest[i + 1:]):
   59         raise DigestInvalidLength.default()