Re: [racket-dev] hex decoding?
Sorry it took so long, but I've submitted a pull request to make this function public in file/sha1: https://github.com/plt/racket/pull/426 Let me know if I screwed up, it's my first pull request. Thanks, Dave On 06/11/2013 05:11 PM, Robby Findler wrote: Yes, I think file/sha1 is the right place. Thanks! Robby On Tue, Jun 11, 2013 at 3:26 PM, David Vanderson david.vander...@gmail.com mailto:david.vander...@gmail.com wrote: Thank you Stephen and Tony for your examples. I found the following private function in db/private/mysql/connection.rkt: (define (hex-string-bytes s) (define (hex-digit-int c) (let ([c (char-integer c)]) (cond [(= (char-integer #\0) c (char-integer #\9)) (- c (char-integer #\0))] [(= (char-integer #\a) c (char-integer #\f)) (+ 10 (- c (char-integer #\a)))] [(= (char-integer #\A) c (char-integer #\F)) (+ 10 (- c (char-integer #\A)))]))) (unless (and (string? s) (even? (string-length s)) (regexp-match? #rx[0-9a-zA-Z]* s)) (raise-type-error 'hex-string-bytes string containing an even number of hexadecimal digits s)) (let* ([c (quotient (string-length s) 2)] [b (make-bytes c)]) (for ([i (in-range c)]) (let ([high (hex-digit-int (string-ref s (+ i i)))] [low (hex-digit-int (string-ref s (+ i i 1)))]) (bytes-set! b i (+ (arithmetic-shift high 4) low b)) Can this function be exported? I'm willing to make a patch with docs and tests - is file/sha1 the right place? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] hex decoding?
Thank you Stephen and Tony for your examples. I found the following private function in db/private/mysql/connection.rkt: (define (hex-string-bytes s) (define (hex-digit-int c) (let ([c (char-integer c)]) (cond [(= (char-integer #\0) c (char-integer #\9)) (- c (char-integer #\0))] [(= (char-integer #\a) c (char-integer #\f)) (+ 10 (- c (char-integer #\a)))] [(= (char-integer #\A) c (char-integer #\F)) (+ 10 (- c (char-integer #\A)))]))) (unless (and (string? s) (even? (string-length s)) (regexp-match? #rx[0-9a-zA-Z]* s)) (raise-type-error 'hex-string-bytes string containing an even number of hexadecimal digits s)) (let* ([c (quotient (string-length s) 2)] [b (make-bytes c)]) (for ([i (in-range c)]) (let ([high (hex-digit-int (string-ref s (+ i i)))] [low (hex-digit-int (string-ref s (+ i i 1)))]) (bytes-set! b i (+ (arithmetic-shift high 4) low b)) Can this function be exported? I'm willing to make a patch with docs and tests - is file/sha1 the right place? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] hex decoding?
On 06/11/2013 04:33 PM, Matthias Felleisen wrote: db/private/mysql/connection.rkt does not export the function, otherwise you could. I don't understand this. I'd like to make the function available to users somewhere - are you saying that's bad? On Jun 11, 2013, at 4:26 PM, David Vanderson wrote: Thank you Stephen and Tony for your examples. I found the following private function in db/private/mysql/connection.rkt: (define (hex-string-bytes s) (define (hex-digit-int c) (let ([c (char-integer c)]) (cond [(= (char-integer #\0) c (char-integer #\9)) (- c (char-integer #\0))] [(= (char-integer #\a) c (char-integer #\f)) (+ 10 (- c (char-integer #\a)))] [(= (char-integer #\A) c (char-integer #\F)) (+ 10 (- c (char-integer #\A)))]))) (unless (and (string? s) (even? (string-length s)) (regexp-match? #rx[0-9a-zA-Z]* s)) (raise-type-error 'hex-string-bytes string containing an even number of hexadecimal digits s)) (let* ([c (quotient (string-length s) 2)] [b (make-bytes c)]) (for ([i (in-range c)]) (let ([high (hex-digit-int (string-ref s (+ i i)))] [low (hex-digit-int (string-ref s (+ i i 1)))]) (bytes-set! b i (+ (arithmetic-shift high 4) low b)) Can this function be exported? I'm willing to make a patch with docs and tests - is file/sha1 the right place? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] hex decoding?
Yes, I think file/sha1 is the right place. Thanks! Robby On Tue, Jun 11, 2013 at 3:26 PM, David Vanderson david.vander...@gmail.comwrote: Thank you Stephen and Tony for your examples. I found the following private function in db/private/mysql/connection.**rkt: (define (hex-string-bytes s) (define (hex-digit-int c) (let ([c (char-integer c)]) (cond [(= (char-integer #\0) c (char-integer #\9)) (- c (char-integer #\0))] [(= (char-integer #\a) c (char-integer #\f)) (+ 10 (- c (char-integer #\a)))] [(= (char-integer #\A) c (char-integer #\F)) (+ 10 (- c (char-integer #\A)))]))) (unless (and (string? s) (even? (string-length s)) (regexp-match? #rx[0-9a-zA-Z]* s)) (raise-type-error 'hex-string-bytes string containing an even number of hexadecimal digits s)) (let* ([c (quotient (string-length s) 2)] [b (make-bytes c)]) (for ([i (in-range c)]) (let ([high (hex-digit-int (string-ref s (+ i i)))] [low (hex-digit-int (string-ref s (+ i i 1)))]) (bytes-set! b i (+ (arithmetic-shift high 4) low b)) Can this function be exported? I'm willing to make a patch with docs and tests - is file/sha1 the right place? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/**dev http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] hex decoding?
Here's the one I've been using in racl (https://github.com/tonyg/racl, raco pkg install racl). It ignores non-hex characters but is less efficient than Stephen's version. Um, and it expects *bytes* as input, not strings. I'm not sure why, actually. (define (hex-string-bytes . strs) (define cleaned (bytes-string/utf-8 (regexp-replace* #rx[^0-9a-fA-F]+ (apply bytes-append strs) ))) (define count (/ (string-length cleaned) 2)) (define bs (make-bytes count 0)) (for ((i (in-range count))) (bytes-set! bs i (string-number (substring cleaned (* i 2) (+ 2 (* i 2))) 16))) bs) On 2013-06-09 8:14 PM, Stephen Chang wrote: There doesn't appear to be a library function for going the reverse direction but here's one way to write it up: #lang racket (define ASCII-ZERO (char-integer #\0)) ;; [0-9A-Fa-f] - Number from 0 to 15 (define (hex-char-number c) (if (char-numeric? c) (- (char-integer c) ASCII-ZERO) (match c [(or #\a #\A) 10] [(or #\b #\B) 11] [(or #\c #\C) 12] [(or #\d #\D) 13] [(or #\e #\E) 14] [(or #\f #\F) 15] [_ (error 'hex-char-number invalid hex char: ~a\n c)]))) (define (hex-string-bytes str) (list-bytes (hex-string-bytelist str))) (define (hex-string-bytelist str) (with-input-from-string str (thunk (let loop () (define c1 (read-char)) (define c2 (read-char)) (cond [(eof-object? c1) null] [(eof-object? c2) (list (hex-char-number c1))] [else (cons (+ (* (hex-char-number c1) 16) (hex-char-number c2)) (loop))]) (require file/sha1) (hex-string-bytes (bytes-hex-string #turtles)) Welcome to DrRacket, version 5.3.4.6 [3m]. Language: racket [custom]. #turtles On Sun, Jun 9, 2013 at 6:30 PM, David Vanderson david.vander...@gmail.com wrote: I'm doing some cryptography exercises that involve a lot of hex encoding/decoding. I see there's a bytes-hex-string function in file/sha1 and openssl/sha1, but I can't find a decode function. Is a hex decode function in the distribution? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev _ Racket Developers list: http://lists.racket-lang.org/dev
[racket-dev] hex decoding?
I'm doing some cryptography exercises that involve a lot of hex encoding/decoding. I see there's a bytes-hex-string function in file/sha1 and openssl/sha1, but I can't find a decode function. Is a hex decode function in the distribution? Thanks, Dave _ Racket Developers list: http://lists.racket-lang.org/dev