content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *    https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  package org.apache.bigtop.manager.stack.core.tarball;
  
  import lombok.extern.slf4j.Slf4j;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.security.MessageDigest;
  
  @Slf4j
  public class ChecksumValidator {
  
      public static boolean validateChecksum(String algorithm, String value, File file) {
          try {
              MessageDigest digest = MessageDigest.getInstance(algorithm.toUpperCase());
              FileInputStream fis = new FileInputStream(file);
              byte[] byteArray = new byte[1024];
              int bytesCount;
  
              while ((bytesCount = fis.read(byteArray)) != -1) {
                  digest.update(byteArray, 0, bytesCount);
              }
              fis.close();
  
              byte[] bytes = digest.digest();
              StringBuilder sb = new StringBuilder();
              for (byte b : bytes) {
                  sb.append(String.format("%02x", b));
              }
  
              String calculatedChecksum = sb.toString();
              return calculatedChecksum.equalsIgnoreCase(value);
          } catch (Exception e) {
              log.error("Error while validating checksum", e);
              return false;
          }
      }
  }
  