日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

checksum命令 linux_数字签名及 Checksum 校验和

發布時間:2024/3/24 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 checksum命令 linux_数字签名及 Checksum 校验和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數字簽名及驗簽

一般情況,為了證明文件或信件從源端通過網絡傳輸到目的端未被人為篡改,通常采用數字簽名的技術。

通過如下簡單幾步實現數據簽名及驗簽。

發送方數字簽名過程:

發送者通過散列函數將帶發送的數據加工成散列值;

發送者用私鑰對散列值進行加密生成一段數字簽名;

發送者將待發送數據、數字簽名、存有公鑰的證書一并發送給接受者。

接受方驗簽過程:

提取出接收到的明文數據,用散列函數加工成散列值;

用從信任簽名者證書中提取的公鑰,將數字簽名字串解密成散列值;

將第一步和第二部中的散列值進行比對,若比對一致則驗簽成功,反之信息被篡改。

下圖是數字簽名及驗簽處理流程圖:

Checksum 校驗和

除了數字簽名之外,有沒有一種簡單的用于在相對安全的環境下,校驗傳送文件完整性的方法呢?

不需要證書也不需要解密,可以簡單通過 Checksum 校驗和實現。

以下說明來自維基百科:

校驗和(英語:Checksum)是冗余校驗的一種形式。 它是通過錯誤檢測方法,對經過空間(如通信)或者時間(如計算機存儲)傳送的數據的完整性進行檢查的一種簡單方法。

計算機領域常見的校驗和的方法有循環冗余校驗(CRC)、MD5、SHA 家族等。

產生校驗和的實際過程一般是向校驗函數或校驗和算法輸入給定的數據,一個良好的校驗和算法通常會對進行很小的修改的輸入數據都會輸出一個顯著不同的值。

一般現在比較常用的是 SHA 家族校驗。

Linux 下的 Checksum 命令

通常發行版的 Linux 版本都自帶了 Checksum 的命令工具,不同發行版命令可能不同。大部分 Linux 版本都包含了 sha1sum 命令工具。

使用非常簡單如下:

$sha1sum {filename} 該命令會輸出Checksum的散列值及文件名

windows 下的 Checksum 命令

windows 默認是沒有自帶 Checksum 命令工具的,如果要實現類似 Linux 里的 sha1sum 命令結果該怎么辦?

微軟還是很厚的地,免費提供了插件供大家下載使用,這個插件名字叫 The File Checksum Integrity Verifier (FCIV),就是一個 fciv.exe 文件。

下面是下載地址:

link to fciv.exe download page!

windows 下 Java 編程將文件 Checksum 值寫入 EOJ 文件

實際就是在 Java 程序里調用 fciv.exe 命令,計算出文件的散列值,再將其追加到 EOJ 文件末尾。使用了 apache common 包里的 exec 類庫。

以下是簡單示例代碼:

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import org.apache.commons.exec.CommandLine;

import org.apache.commons.exec.DefaultExecutor;

import org.apache.commons.exec.PumpStreamHandler;

/**** The checksum class implements below functions:* read the EOJ file for each record* call the fciv.exe command capture the checksum value* and write into the EOJ file** @author lee**/

public class CheckSumClass {

/*** @param args*/

public static void main(String[] args) {

//EOJ file full name including the file pathString eojFile = args[0];

//fciv.exe file pathString cksumFile = args[1];

//flat file pathString flatfilePath = args[2];

if(null==eojFile||null==cksumFile||null==flatfilePath

||"".equals(eojFile)||"".equals(cksumFile)

||"".equals(flatfilePath)){

System.err.println("The error parameters!");

System.exit(-1);

}

FileInputStream fi = null;

PrintWriter pw = null;

File file = null;

Scanner in = null;

List cksumList = new ArrayList();

try{

fi = new FileInputStream(eojFile);

file = new File(eojFile);

in = new Scanner(fi);

String currentLine = null;

while(in.hasNextLine()){

currentLine = in.nextLine();

if(currentLine!=null&&!"".equals(currentLine.trim())){

String[] tokens = currentLine.split("~");

String fileName = tokens[0];

//execute fciv.exe command and capture result value put into the cksumListByteArrayOutputStream outputStream = new ByteArrayOutputStream();

String command = cksumFile+" -wp -sha1 "+flatfilePath+"\\"+fileName;

CommandLine commandline = CommandLine.parse(command);

DefaultExecutor exec = new DefaultExecutor();

PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);

exec.setStreamHandler(streamHandler);

exec.execute(commandline);

String output = outputStream.toString();

String outstrs[] = output.split("\r\n");

String shavalue = outstrs[3].split(" ")[0];

cksumList.add(currentLine+"~"+shavalue);

}

}

//write the checksum value into EOJ filepw = new PrintWriter(file);

for(int j=0;j

pw.println(cksumList.get(j));

}

pw.flush();

}catch(Exception ex){

ex.printStackTrace();

}finally{

if(fi!=null)

try {

fi.close();

} catch (IOException e) {

// TODO Auto-generated catch blocke.printStackTrace();

}

if(pw!=null) pw.close();

if(in!=null) in.close();

if(file!=null) file = null;

}

}

}

總結

以上是生活随笔為你收集整理的checksum命令 linux_数字签名及 Checksum 校验和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。