com.branch.cos.utils.Sha1Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of branch_cos_api Show documentation
Show all versions of branch_cos_api Show documentation
branch java sdk for qcloud cos
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
* According to cos feature, we modify some class,comment, field name, etc.
*/
package com.branch.cos.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
public class Sha1Utils {
public static byte[] computeSHA1Hash(InputStream is) throws IOException {
return DigestUtils.sha1(is);
}
/**
* Returns the SHA1 in base64 for the data from the given input stream. Note this method closes
* the given input stream upon completion.
*/
public static String sha1AsBase64(InputStream is) throws IOException {
return Base64.encodeAsString(computeSHA1Hash(is));
}
/**
* Computes the SHA1 hash of the given data and returns it as an array of bytes.
*/
public static byte[] computeSHA1Hash(byte[] input) {
return DigestUtils.sha1(input);
}
/**
* Returns the SHA1 in base64 for the given byte array.
*/
public static String sha1AsBase64(byte[] input) {
return Base64.encodeAsString(computeSHA1Hash(input));
}
public static String sha1Hex(File file) throws FileNotFoundException, IOException {
return Hex.encodeHexString(computeSHA1Hash(file));
}
public static String sha1Hex(String utf8Content) {
return Hex.encodeHexString(computeSHA1Hash(utf8Content.getBytes(StringUtils.UTF8)));
}
public static String sha1Hex(byte[] input) {
return Hex.encodeHexString(computeSHA1Hash(input));
}
/**
* Computes the SHA1 of the given file.
*/
public static byte[] computeSHA1Hash(File file) throws FileNotFoundException, IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return computeSHA1Hash(input);
} finally {
if (input != null) {
input.close();
}
}
}
/**
* Returns the SHA1 in base64 for the given file.
*/
public static String sha1AsBase64(File file) throws FileNotFoundException, IOException {
return Base64.encodeAsString(computeSHA1Hash(file));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy