Skip to content

Instantly share code, notes, and snippets.

@jitomesky
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitomesky/451ebdd4d6b14f569e47 to your computer and use it in GitHub Desktop.
Save jitomesky/451ebdd4d6b14f569e47 to your computer and use it in GitHub Desktop.
printfで8bitずつ出力されない謎コード
# ----------------------------------------------------------------
# environment
CC = gcc
# ----------------------------------------------------------------
# options
CFLAGS = -std=c99 -lcrypto
# ----------------------------------------------------------------
# sources and objects
C_SRC = sha512test.c
C_OBJ = $(C_SRC:.c=)
# ----------------------------------------------------------------
# executables
EXEC = $(C_OBJ)
all: $(EXEC)
$(C_OBJ): $(C_SRC)
$(CC) -o $@ $(CFLAGS) $(C_SRC)
# ----------------------------------------------------------------
# rules
.c.:
$(CC) -o $* $(CFLAGS) -c $<
# ----------------------------------------------------------------
# clean up
clean:
/bin/rm -f $(EXEC)
# ----------------------------------------------------------------
# End of Makefile
## 修正前
$ ./sha2512test
ffffffdbffffffb5237ffffffad3fffffffa5ffffffb818ffffffb8ffffffeeffffffcaffffff9cffffffa25a47ef29517dffffffb2ffffffb25f4affffff8dffffffb5fffffff717ffffffffffffff90ffffffbfb7effffff94ffffffef4f5c4e313dfffffffb6ffffffe4ffffff8fffffffbdffffff9a2e4079596ffffffa75c47cffffffdb61ffffff9cfffffff9ffffffc2ffffffd4fffffff6ffffffd9
$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
## 修正後
$ ./hprng
dbb5237ad3fa5b818b8eeca9ca25a47ef29517db2b25f4a8db5f717ff90bfb7e94ef4f5c4e313dfb6e48fbd9a2e4079596a75c47cdb619cf9c2d4f6d9
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <openssl/sha.h>
int main(void){
char hoge[]= "hoge";
char hash[64];
SHA512(hoge, strlen(hoge), hash);
for(int i=0;i < 64; i++){
// hhは長さ指定子
// http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/printf.3.html
printf("%02hhx", hash[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment