IT

이진 파일을 비교하여 동일한 지 확인하는 방법은 무엇입니까?

lottoking 2020. 6. 8. 08:06
반응형

이진 파일을 비교하여 동일한 지 확인하는 방법은 무엇입니까?


두 개의 이진 파일이 동일한 지 아닌지를 아는 가장 쉬운 방법 (우분투 리눅스에서 그래픽 도구 또는 명령 줄 사용)은 무엇입니까 (타임 스탬프 제외)? 실제로 차이를 추출 할 필요는 없습니다. 나는 그들이 같은지 아닌지를 알아야합니다.


표준 유닉스 diff는 파일이 동일한 지 여부를 보여줍니다.

[me@host ~]$ diff 1.bin 2.bin
Binary files 1.bin and 2.bin differ

명령에서 출력이 없으면 파일에 차이가 없음을 의미합니다.


cmp명령을 사용하십시오 . 이진이 같으면 깨끗하게 종료되거나 첫 번째 차이점이 발생한 위치를 인쇄하고 종료합니다.


Visual Binary Diff 가 내가 찾던 것임을 알았 습니다.

  • 우분투 : apt-get install vbindiff
  • 아치 리눅스 : pacman -S vbindiff
  • Mac OS X : MacPortsport install vbindiff 를 통해 .
  • Mac OS X : brew install vbindiffHomebrew를 통해

sha1을 사용하여 체크섬을 생성하십시오.

sha1 [FILENAME1]
sha1 [FILENAME2]

바이너리 파일을 16 진수 표현으로 변환하기 위해 hexdump를 사용하여 meld / kompare / 다른 diff 도구로 열었습니다. 당신과 달리 나는 파일의 차이를 겪었습니다.

hexdump tmp/Circle_24.png > tmp/hex1.txt
hexdump /tmp/Circle_24.png > tmp/hex2.txt

meld tmp/hex1.txt tmp/hex2.txt

cmp 명령을 사용하십시오. 자세한 내용은 이진 파일 및 텍스트 비교 를 참조하십시오.

cmp -b file1 file2

md5 <filename1>

md5 <filename2>

그들이 같은지 확인하십시오 :-)


플래시 메모리 결함을 찾기 위해 차이점을 포함하는 모든 1K 블록을 보여주는이 스크립트를 작성해야했습니다 (첫 번째 블록뿐만 아니라 cmp -b)

#!/bin/sh

f1=testinput.dat
f2=testoutput.dat

size=$(stat -c%s $f1)
i=0
while [ $i -lt $size ]; do
  if ! r="`cmp -n 1024 -i $i -b $f1 $f2`"; then
    printf "%8x: %s\n" $i "$r"
  fi
  i=$(expr $i + 1024)
done

산출:

   2d400: testinput.dat testoutput.dat differ: byte 3, line 1 is 200 M-^@ 240 M- 
   2dc00: testinput.dat testoutput.dat differ: byte 8, line 1 is 327 M-W 127 W
   4d000: testinput.dat testoutput.dat differ: byte 37, line 1 is 270 M-8 260 M-0
   4d400: testinput.dat testoutput.dat differ: byte 19, line 1 is  46 &  44 $

면책 조항 : 5 분 안에 스크립트를 해킹했습니다. 명령 행 인수를 지원하지 않으며 파일 이름에서 공백을 지원하지 않습니다.


Diff with the following options would do a binary comparison to check just if the files are different at all and it'd output if the files are the same as well:

diff -qs {file1} {file2}

If you are comparing two files with the same name in different directories, you can use this form instead:

diff -qs {file1} --to-file={dir2}

OS X El Capitan


Try diff -s

Short answer: run diff with the -s switch.

Long answer: read on below.


Here's an example. Let's start by creating two files with random binary contents:

$ dd if=/dev/random bs=1k count=1 of=test1.bin
1+0 records in
1+0 records out
1024 bytes (1,0 kB, 1,0 KiB) copied, 0,0100332 s, 102 kB/s


$ dd if=/dev/random bs=1k count=1 of=test2.bin
1+0 records in
1+0 records out
1024 bytes (1,0 kB, 1,0 KiB) copied, 0,0102889 s, 99,5 kB/s

Now let's make a copy of the first file:

$ cp test1.bin copyoftest1.bin

Now test1.bin and test2.bin should be different:

$ diff test1.bin test2.bin
Binary files test1.bin and test2.bin differ

... and test1.bin and copyoftest1.bin should be identical:

$ diff test1.bin copyoftest1.bin

But wait! Why is there no output?!?

The answer is: this is by design. There is no output on identical files.

But there are different error codes:

$ diff test1.bin test2.bin
Binary files test1.bin and test2.bin differ

$ echo $?
1


$ diff test1.bin copyoftest1.bin

$ echo $?
0

Now fortunately you don't have to check error codes each and every time because you can just use the -s (or --report-identical-files) switch to make diff be more verbose:

$ diff -s test1.bin copyoftest1.bin
Files test1.bin and copyoftest1.bin are identical

Radiff2 is a tool designed to compare binary files, similar to how regular diff compares text files.

Try radiff2 which is a part of radare2 disassembler. For instance, with this command:

radiff2 -x file1.bin file2.bin

You get pretty formatted two columns output where differences are highlighted.


md5sum binary1 binary2

If the md5sum is same, binaries are same

E.g

md5sum new*
89c60189c3fa7ab5c96ae121ec43bd4a  new.txt
89c60189c3fa7ab5c96ae121ec43bd4a  new1.txt
root@TinyDistro:~# cat new*
aa55 aa55 0000 8010 7738
aa55 aa55 0000 8010 7738


root@TinyDistro:~# cat new*
aa55 aa55 000 8010 7738
aa55 aa55 0000 8010 7738
root@TinyDistro:~# md5sum new*
4a7f86919d4ac00c6206e11fca462c6f  new.txt
89c60189c3fa7ab5c96ae121ec43bd4a  new1.txt

My favourite ones using xxd hex-dumper from the vim package :

1) using vimdiff (part of vim)

#!/bin/bash
FILE1="$1"
FILE2="$2"
vimdiff <( xxd "$FILE1" ) <( xxd "$FILE2" )

2) using diff

#!/bin/bash
FILE1=$1
FILE2=$2
diff -W 140 -y <( xxd $FILE1 ) <( xxd $FILE2 ) | colordiff | less -R -p '  \|  '

There is a relatively simple way to check if two binary files are the same.

If you use file input/output in a programming language; you can store each bit of both the binary files into their own arrays.

At this point the check is as simple as :

if(file1 != file2){
    //do this
}else{
    /do that
}

참고URL : https://stackoverflow.com/questions/12118403/how-to-compare-binary-files-to-check-if-they-are-the-same

반응형