암호화 해싱

암호화 해싱 (Cryptographic Hashing)

바이트 시퀀스의 SHA-1, SHA-224, SHA-256 해시를 계산하는 절차들을 설명합니다. 보안 목적에서는 SHA-2 계열인 sha224-bytessha256-bytes를 선호해야 합니다.

출처: Racket Reference

본문

procedure

(sha1-bytes in [start end]) → bytes?
  in : (or/c bytes? input-port?)
  start : exact-nonnegative-integer? = 0
  end : (or/c #f exact-nonnegative-integer?) = #f

procedure

(sha224-bytes in [start end]) → bytes?
  in : (or/c bytes? input-port?)
  start : exact-nonnegative-integer? = 0
  end : (or/c #f exact-nonnegative-integer?) = #f

procedure

(sha256-bytes in [start end]) → bytes?
  in : (or/c bytes? input-port?)
  start : exact-nonnegative-integer? = 0
  end : (or/c #f exact-nonnegative-integer?) = #f

바이트 시퀀스의 SHA-1, SHA-224, SHA-256 해시를 계산하고, 각각 20바이트, 28바이트, 32바이트인 바이트 문자열로 해시를 반환합니다.

startend 인자는 해시 계산에 사용되는 입력의 바이트 범위를 결정합니다. end 값이 #f이면 바이트 문자열의 끝 또는 입력 포트의 파일 끝 위치에 해당합니다. in이 바이트 문자열일 때, startend 값(둘 다 #f가 아닐 때)은 바이트 문자열의 길이보다 크면 안 되며, startend보다 크면 안 됩니다. in이 입력 포트일 때, startend보다 크면 안 됩니다. in이 파일 끝 이전에 start 또는 end보다 적은 바이트를 공급하면, start 및/또는 end는 실제로 공급된 바이트 수로 바뀝니다(그래서 빈 또는 잘린 바이트 시퀀스가 해시됩니다). in이 입력 포트이고 end가 숫자이면, 입력 포트에서 기껏해야 end 바이트가 읽힙니다.

보안 목적을 위해, sha224-bytessha256-bytes(SHA-2 계열에 속함)를 sha1-bytes보다 선호하세요.

바이트 문자열 해시를 사람이 읽을 수 있는 문자열로 바꾸려면 file/sha1bytes->hex-string을 사용하세요.

예시:

> (sha1-bytes #"abc")

#"\251\231>6G\6\201j\272>%qxP\302l\234\320\330\235"
> (require file/sha1)
> (bytes->hex-string (sha1-bytes #"abc"))

"a9993e364706816aba3e25717850c26c9cd0d89d"
> (bytes->hex-string (sha224-bytes #"abc"))

"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"
> (bytes->hex-string (sha224-bytes (open-input-string "xabcy") 1 4))

"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"

base 패키지의 7.0.0.5 버전에서 추가되었습니다.

더 알아보기

  • file/sha1bytes->hex-string
  • 바이트 문자열(bytes?)과 입력 포트(input-port?) 관련 문서