► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 13 | Input and Output

13.1 | Ports
13.2 | Byte and String Input
13.3 | Byte and String Output
13.4 | Reading
13.5 | Writing
13.6 | Pretty Printing
13.7 | Reader Extension
13.8 | Printer Extension
13.9 | Serialization
13.10 | Fast-Load Serialization
13.11 | Cryptographic Hashing

On this page:

sha1-bytes
sha224-bytes
sha256-bytes

top ← prev up next →

#### 13.11`` Cryptographic Hashing

> > 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

Computes the SHA-1, SHA-224, or SHA-256 hash of a byte sequence and returns the hash as a byte string with 20 bytes, 28 bytes, or 32 bytes, respectively.

The start and end arguments determine the range of bytes of the input that are used to compute the hash. An end value of #f corresponds to the end of the byte string or an end-of-file position for an input port. When in is a byte string, the start and end values (when non #f) must be no greater than the length of the byte string, and start must be no greater than end. When in is an input port, start must be no greater than end; if in supplies less than start or end bytes before an end-of-file, then start and/or end is effectively changed to the number of supplied bytes (so that an empty or truncated byte sequence is hashed). When in is an input port and end is a number, then at most end bytes are read from the input port.

For security purposes, favor sha224-bytes and sha256-bytes (which are part of the SHA-2 family) over sha1-bytes.

Use bytes->hex-string from file/sha1 to convert a byte string hash to a human-readable string.

Examples:

> > (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"

Added in version 7.0.0.5 of package base.

top ← prev up next →