Operation System/General

파일 정합성 확인 (MD5, SHA1, SHA2, HASH)

junsuyoun 2022. 9. 1. 23:36
728x90
반응형

저장 매체 또는 네트워크를 통해 파일을 복사(or 이동) 하다 보면 파일이 깨지는 경우가 종종 있습니다. 이러한 문제의 원인은 파일을 옮기는 과정에서 발생할 수 있는 손상인데요. 해쉬 함수를 통해 파일의 정합성을 확인하여 원본 파일과 복사한 파일에 문제가 없음을 알아보도록 하겠습니다.


파일 준비

$ cat testfile 
a b c d e f g h i j k l m n o p q r s t u v w x y z

openssl

설치 및 확인

 

########################################
# 미설치
########################################
$ whereis openssl
openssl:

########################################
# 설치 확인
########################################
$ whereis openssl
openssl: /usr/bin/openssl /usr/lib64/openssl

 

설치 방법
## CentOS / RHEL
# yum install openssl

## Ubuntu
# apt-get install openssl

## OS X
# brew install openssl
  • openssl 유틸리티가 설치되어 있지 않다면 OS 별 패키지 매니저를 이용해서 다운 받습니다.
  • 패키지 매니저를 사용할 수 없는 환경이라면 직접 패키지 파일을 받아 설치하여 사용 합니다.
 

/index.html

Welcome to OpenSSL! The OpenSSL Project develops and maintains the OpenSSL software - a robust, commercial-grade, full-featured toolkit for general-purpose cryptography and secure communication. The project’s technical decision making is managed by the O

www.openssl.org

 

사용 방법
########################################
# openssl 유틸리티에서 지원하는 해쉬 알고리즘
########################################
$ openssl <command> <file>
Message Digest commands (see the dgst command for more details)
gost-mac          md4               md5               md_gost94         
ripemd160         sha1              sha224            sha256            
sha384            sha512            streebog256       streebog512       
whirlpool

########################################
# OS X
########################################
% openssl md5 testfile
MD5(testfile)= 600d7deb49d6acf60eee378faa12621e

% openssl sha1 testfile
SHA1(testfile)= 01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753

########################################
# CentOS
########################################
$ openssl md5 testfile
MD5(testfile)= 600d7deb49d6acf60eee378faa12621e

$ openssl sha1 testfile
SHA1(testfile)= 01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753

########################################
# Ubuntu
########################################
$ openssl md5 testfile 
MD5(testfile)= 600d7deb49d6acf60eee378faa12621e

$ openssl sha1 testfile 
SHA1(testfile)= 01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753

 


MD5, SHA1, SHA2

사용 방법
########################################
# OS X
########################################
% md5 testfile   
MD5 (testfile) = 600d7deb49d6acf60eee378faa12621e

% shasum -a 1 testfile
01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753  testfile

% shasum -a 256 testfile
4da0a373cc2f735d8c0dead1aa538b8cded301774f4ab058c6bfead5a585ccdb  testfile

########################################
# shasum 알고리즘 옵션
########################################
% shasum --help
Usage: shasum [OPTION]... [FILE]...
... 중략 ...
  -a, --algorithm   1 (default), 224, 256, 384, 512, 512224, 512256
... 생략 ...



########################################
# Windows (10, 11)
########################################
PS C:\work> certutil.exe -hashfile -?
사용법:
  CertUtil [옵션] -hashfile InFile [HashAlgorithm]
  파일에 암호화 해시 생성 및 표시

... 중략 ...

해시 알고리즘: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512

... 중략 ...

PS C:\work> certutil.exe -hashfile .\testfile.txt md5
MD5의 .\testfile.txt 해시:
9e1dbcc77f238615690eb3a56245cf50
CertUtil: -hashfile 명령이 성공적으로 완료되었습니다.

PS C:\work> certutil.exe -hashfile .\testfile.txt sha1
SHA1의 .\testfile.txt 해시:
2e93a4d6ff85945373f236a170f9fdf31524ea0c
CertUtil: -hashfile 명령이 성공적으로 완료되었습니다.

PS C:\work> certutil.exe -hashfile .\testfile.txt sha256
SHA256의 .\testfile.txt 해시:
58cba4bfa47c025d17ca628912a71c9d3c7dc2b0a40dcb18a911696ab78c1225
CertUtil: -hashfile 명령이 성공적으로 완료되었습니다.

########################################
# CentOS md5sum , sha..sum
########################################
$ md5sum testfile 
600d7deb49d6acf60eee378faa12621e  testfile

$ sha1sum testfile 
01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753  testfile

$ sha256sum testfile 
4da0a373cc2f735d8c0dead1aa538b8cded301774f4ab058c6bfead5a585ccdb  testfile

########################################
# Ubuntu md5sum , sha..sum
########################################
$ md5sum testfile 
600d7deb49d6acf60eee378faa12621e  testfile

$ shasum -a 1 testfile
01ac9d6d397e8cfd6f9d20ec4c7dda72c6b6c753  testfile

$ shasum -a 256 testfile
4da0a373cc2f735d8c0dead1aa538b8cded301774f4ab058c6bfead5a585ccdb  testfile

########################################
# AIX
########################################
csum -h md5

########################################
# UX
########################################
openssl 사용가능

########################################
# Solaris
########################################
digest -l
gigest -a md5 -v <file>
  • 기본적으로 OS에 내장되어 있는 해쉬 함수 유틸리티를 활용하여 사용 가능 합니다.
  • 해쉬 함수를 구하는 유틸리티가 다르더라도 알고리즘이 같다면 결과는 같습니다. (md5, sha1, sha256 등등)
  • UNIX 환경에서는 완전한 테스트를 해보진 못했지만 유닉스 접근이 가능했을 때 테스트 해봤던 명령어 입니다.

 

응용 해보기

  • 일반적으로 파일 반입은 CD/DVD를 많이 활용 합니다.
  • 원본 파일의 해쉬 함수 확인
  • 저장 매체에 파일을 복사한 후의 저장 매체 경로의 파일 해쉬 함수 확인
  • 저장 매체를 이용하여 파일을 복사한 경로에서 파일 해쉬 함수 확인

해쉬함수를 통해 파일의 손상 여부를 파악할 수 있으면 파일 반입 전/후 문제에 대해 파악할 수 있습니다.

대표적으로 CD/DVD 굽는 과정에서 파일이 손상 여부, 파일 반입 후 암호화 보안 프로그램에 의해 암호화가 되었는지 등등 

728x90
반응형