css securitylab & fop parser
This commit is contained in:
parent
7bbf3a5070
commit
1eadfe70da
5 changed files with 283 additions and 0 deletions
102
infix-parser-left-assoc-2.rkt
Normal file
102
infix-parser-left-assoc-2.rkt
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
||||||
|
;; about the language level of this file in a form that our tools can easily process.
|
||||||
|
#reader(lib "htdp-advanced-reader.ss" "lang")((modname infix-parser-left-assoc-2) (read-case-sensitive #t) (teachpacks ((lib "gui.rkt" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "gui.rkt" "teachpack" "htdp")) #f)))
|
||||||
|
(define (alt2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[else (parser2 lst)])))
|
||||||
|
|
||||||
|
(define (alt3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[(not (empty? (parser2 lst))) (parser2 lst)]
|
||||||
|
[else (parser3 lst)])))
|
||||||
|
|
||||||
|
(define (seq2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst)))))
|
||||||
|
(second (parser2 (second (parser1 lst)))))])))
|
||||||
|
|
||||||
|
(define (seq3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[(empty? (parser3 (second (parser2 (second (parser1 lst)))))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst))))
|
||||||
|
(first (parser3 (second (parser2 (second (parser1 lst)))))))
|
||||||
|
(second (parser3 (second (parser2 (second (parser1 lst)))))))])))
|
||||||
|
|
||||||
|
(define (accept-itm itm)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(equal? (first lst) itm) (list itm (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-digit)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(or (equal? "0" (first lst))
|
||||||
|
(equal? "1" (first lst))
|
||||||
|
(equal? "2" (first lst))
|
||||||
|
(equal? "3" (first lst))
|
||||||
|
(equal? "4" (first lst))
|
||||||
|
(equal? "5" (first lst))
|
||||||
|
(equal? "6" (first lst))
|
||||||
|
(equal? "7" (first lst))
|
||||||
|
(equal? "8" (first lst))
|
||||||
|
(equal? "9" (first lst)))
|
||||||
|
(list (first lst) (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-empty)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) (list empty empty)]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define (infix->prefix str)
|
||||||
|
(infix->prefix-expl (explode str)))
|
||||||
|
|
||||||
|
(define (infix->prefix-expl lst)
|
||||||
|
(cond [(empty? (parse-add-sub lst)) empty]
|
||||||
|
[else (tree->prefix (first (parse-add-sub lst)))]))
|
||||||
|
|
||||||
|
(define (parse-add-sub lst)
|
||||||
|
(parse-add-sub-x (second (parse-mul-div lst))
|
||||||
|
(first (parse-mul-div lst))))
|
||||||
|
|
||||||
|
(define (parse-add-sub-x lst expr)
|
||||||
|
(cond [(not (empty? ((accept-itm "+") lst))) (parse-add-sub-x (second (parse-mul-div (second ((accept-itm "+") lst))))
|
||||||
|
(list expr "+" (first (parse-mul-div (second ((accept-itm "+") lst))))))]
|
||||||
|
[(not (empty? ((accept-itm "-") lst))) (parse-add-sub-x (second (parse-mul-div (second ((accept-itm "-") lst))))
|
||||||
|
(list expr "-" (first (parse-mul-div (second ((accept-itm "-") lst))))))]
|
||||||
|
[else (list expr lst)]))
|
||||||
|
|
||||||
|
(define (parse-mul-div lst)
|
||||||
|
(parse-mul-div-x (second (parse-number lst))
|
||||||
|
(first (parse-number lst))))
|
||||||
|
|
||||||
|
(define (parse-mul-div-x lst expr)
|
||||||
|
(cond [(not (empty? ((accept-itm "*") lst))) (parse-mul-div-x (second (parse-number (second ((accept-itm "*") lst))))
|
||||||
|
(list expr "*" (first (parse-number (second ((accept-itm "*") lst))))))]
|
||||||
|
[(not (empty? ((accept-itm "/") lst))) (parse-mul-div-x (second (parse-number (second ((accept-itm "/") lst))))
|
||||||
|
(list expr "/" (first (parse-number (second ((accept-itm "/") lst))))))]
|
||||||
|
[else (list expr lst)]))
|
||||||
|
|
||||||
|
(define parse-number (accept-digit))
|
||||||
|
|
||||||
|
|
||||||
|
(define (tree->prefix tree)
|
||||||
|
(cond [(string? tree) tree]
|
||||||
|
[else (string-append (second tree)
|
||||||
|
(tree->prefix (first tree))
|
||||||
|
(tree->prefix (third tree)))]))
|
||||||
89
infix-parser.rkt
Normal file
89
infix-parser.rkt
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
||||||
|
;; about the language level of this file in a form that our tools can easily process.
|
||||||
|
#reader(lib "htdp-advanced-reader.ss" "lang")((modname infix-parser) (read-case-sensitive #t) (teachpacks ((lib "gui.rkt" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "gui.rkt" "teachpack" "htdp")) #f)))
|
||||||
|
(define (alt2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[else (parser2 lst)])))
|
||||||
|
|
||||||
|
(define (alt3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[(not (empty? (parser2 lst))) (parser2 lst)]
|
||||||
|
[else (parser3 lst)])))
|
||||||
|
|
||||||
|
(define (seq2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst)))))
|
||||||
|
(second (parser2 (second (parser1 lst)))))])))
|
||||||
|
|
||||||
|
(define (seq3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[(empty? (parser3 (second (parser2 (second (parser1 lst)))))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst))))
|
||||||
|
(first (parser3 (second (parser2 (second (parser1 lst)))))))
|
||||||
|
(second (parser3 (second (parser2 (second (parser1 lst)))))))])))
|
||||||
|
|
||||||
|
(define (accept-itm itm)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(equal? (first lst) itm) (list itm (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-digit)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(or (equal? "0" (first lst))
|
||||||
|
(equal? "1" (first lst))
|
||||||
|
(equal? "2" (first lst))
|
||||||
|
(equal? "3" (first lst))
|
||||||
|
(equal? "4" (first lst))
|
||||||
|
(equal? "5" (first lst))
|
||||||
|
(equal? "6" (first lst))
|
||||||
|
(equal? "7" (first lst))
|
||||||
|
(equal? "8" (first lst))
|
||||||
|
(equal? "9" (first lst)))
|
||||||
|
(list (first lst) (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-empty)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) (list empty empty)]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define (infix->prefix str)
|
||||||
|
(infix->prefix-expl (explode str)))
|
||||||
|
|
||||||
|
(define (infix->prefix-expl lst)
|
||||||
|
(cond [(empty? (parse-add-sub lst)) empty]
|
||||||
|
[else (tree->prefix (first (parse-add-sub lst)))]))
|
||||||
|
|
||||||
|
(define (parse-add-sub lst)
|
||||||
|
((alt3 (seq3 parse-mul-div (accept-itm "+") parse-add-sub)
|
||||||
|
(seq3 parse-mul-div (accept-itm "-") parse-add-sub)
|
||||||
|
parse-mul-div) lst))
|
||||||
|
|
||||||
|
(define (parse-mul-div lst)
|
||||||
|
((alt3 (seq3 parse-number (accept-itm "*") parse-mul-div)
|
||||||
|
(seq3 parse-number (accept-itm "/") parse-mul-div)
|
||||||
|
parse-number) lst))
|
||||||
|
|
||||||
|
(define parse-number (accept-digit))
|
||||||
|
|
||||||
|
(define (tree->prefix tree)
|
||||||
|
(cond [(string? tree) tree]
|
||||||
|
[else (string-append (second tree)
|
||||||
|
(tree->prefix (first tree))
|
||||||
|
(tree->prefix (third tree)))]))
|
||||||
24
onetimepad.rkt
Normal file
24
onetimepad.rkt
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#lang racket
|
||||||
|
|
||||||
|
(define c1 (list #x32 #x1b #x61 #x49 #x48 #x1c #x03 #x2a #x1e #x06 #x5e #x32 #x28 #x24 #x5c #x24))
|
||||||
|
(define c2 (list #x1a #x12 #x12 #x3c #x48 #x39 #x58 #x01 #x39 #x41 #x50 #x41 #x2a #x18 #x5c #x1c))
|
||||||
|
(define c3 (list #x1c #x1c #x22 #x2d #x47 #x5f #x29 #x29 #x37 #x78 #x02 #x26 #x2b #x3e #x08 #x1e))
|
||||||
|
|
||||||
|
(define known-str (bytes->list #"js61xumi"))
|
||||||
|
(define known-str-len (length known-str))
|
||||||
|
|
||||||
|
(define key-part-1 (map bitwise-xor known-str (take c1 known-str-len)))
|
||||||
|
(define key-part-2 (map bitwise-xor known-str (drop c2 known-str-len)))
|
||||||
|
(define key (append key-part-1 key-part-2))
|
||||||
|
|
||||||
|
(define (decrypt bytes key)
|
||||||
|
(map bitwise-xor bytes key))
|
||||||
|
|
||||||
|
(define m1 (decrypt c1 key))
|
||||||
|
(define m2 (decrypt c2 key))
|
||||||
|
(define m3 (decrypt c3 key))
|
||||||
|
|
||||||
|
(displayln (list->bytes m1))
|
||||||
|
(displayln (list->bytes m2))
|
||||||
|
(displayln (list->bytes m3))
|
||||||
|
(displayln (list->bytes key))
|
||||||
58
parser-utils.rkt
Normal file
58
parser-utils.rkt
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
||||||
|
;; about the language level of this file in a form that our tools can easily process.
|
||||||
|
#reader(lib "htdp-advanced-reader.ss" "lang")((modname parser-utils) (read-case-sensitive #t) (teachpacks ((lib "gui.rkt" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "gui.rkt" "teachpack" "htdp")) #f)))
|
||||||
|
(define (alt2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[else (parser2 lst)])))
|
||||||
|
|
||||||
|
(define (alt3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(not (empty? (parser1 lst))) (parser1 lst)]
|
||||||
|
[(not (empty? (parser2 lst))) (parser2 lst)]
|
||||||
|
[else (parser3 lst)])))
|
||||||
|
|
||||||
|
(define (seq2 parser1 parser2)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst)))))
|
||||||
|
(second (parser2 (second (parser1 lst)))))])))
|
||||||
|
|
||||||
|
(define (seq3 parser1 parser2 parser3)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? (parser1 lst)) empty]
|
||||||
|
[(empty? (parser2 (second (parser1 lst)))) empty]
|
||||||
|
[(empty? (parser3 (second (parser2 (second (parser1 lst)))))) empty]
|
||||||
|
[else (list (list (first (parser1 lst))
|
||||||
|
(first (parser2 (second (parser1 lst))))
|
||||||
|
(first (parser3 (second (parser2 (second (parser1 lst)))))))
|
||||||
|
(second (parser3 (second (parser2 (second (parser1 lst)))))))])))
|
||||||
|
|
||||||
|
(define (accept-itm itm)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(equal? (first lst) itm) (list itm (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-digit)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) empty]
|
||||||
|
[(or (equal? "0" (first lst))
|
||||||
|
(equal? "1" (first lst))
|
||||||
|
(equal? "2" (first lst))
|
||||||
|
(equal? "3" (first lst))
|
||||||
|
(equal? "4" (first lst))
|
||||||
|
(equal? "5" (first lst))
|
||||||
|
(equal? "6" (first lst))
|
||||||
|
(equal? "7" (first lst))
|
||||||
|
(equal? "8" (first lst))
|
||||||
|
(equal? "9" (first lst)))
|
||||||
|
(list (first lst) (rest lst))]
|
||||||
|
[else empty])))
|
||||||
|
|
||||||
|
(define (accept-empty)
|
||||||
|
(lambda (lst)
|
||||||
|
(cond [(empty? lst) (list empty empty)]
|
||||||
|
[else empty])))
|
||||||
10
rsa-sign.rkt
Normal file
10
rsa-sign.rkt
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
#lang racket
|
||||||
|
|
||||||
|
(require math)
|
||||||
|
|
||||||
|
(define h 4294967295)
|
||||||
|
(define n 67336047553)
|
||||||
|
(define e 699863)
|
||||||
|
(define d 2891856227)
|
||||||
|
|
||||||
|
(with-modulus n (modexpt h d))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue