github.com/cs3org/reva/v2@v2.27.7/pkg/crypto/crypto.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package crypto
    20  
    21  import (
    22  	"crypto/md5"
    23  	"crypto/sha1"
    24  	"fmt"
    25  	"hash/adler32"
    26  	"io"
    27  )
    28  
    29  // ComputeMD5XS computes the MD5 checksum.
    30  func ComputeMD5XS(r io.Reader) (string, error) {
    31  	h := md5.New()
    32  	if _, err := io.Copy(h, r); err != nil {
    33  		return "", err
    34  	}
    35  
    36  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    37  }
    38  
    39  // ComputeAdler32XS computes the adler32 checksum.
    40  func ComputeAdler32XS(r io.Reader) (string, error) {
    41  	h := adler32.New()
    42  	if _, err := io.Copy(h, r); err != nil {
    43  		return "", err
    44  	}
    45  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    46  }
    47  
    48  // ComputeSHA1XS computes the sha1 checksum.
    49  func ComputeSHA1XS(r io.Reader) (string, error) {
    50  	h := sha1.New()
    51  	if _, err := io.Copy(h, r); err != nil {
    52  		return "", err
    53  	}
    54  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    55  }