summaryrefslogtreecommitdiff
path: root/the-ideal-stocking-stuffer/tiss.scm
diff options
context:
space:
mode:
Diffstat (limited to 'the-ideal-stocking-stuffer/tiss.scm')
-rw-r--r--the-ideal-stocking-stuffer/tiss.scm39
1 files changed, 39 insertions, 0 deletions
diff --git a/the-ideal-stocking-stuffer/tiss.scm b/the-ideal-stocking-stuffer/tiss.scm
new file mode 100644
index 0000000..5162b40
--- /dev/null
+++ b/the-ideal-stocking-stuffer/tiss.scm
@@ -0,0 +1,39 @@
+(define-module (tiss)
+ #:use-module (ice-9 string-fun)
+ #:use-module (hashing md5)
+ #:use-module (rnrs bytevectors)
+ #:use-module (ice-9 exceptions)
+ #:export (tiss
+ get-hash
+ starts-with?))
+
+
+(define (tiss prefix output-prefix suffix)
+ "Given KEY puzzle key and the desired
+OUTPUT-PREFIX, and the current suffix,
+incrementally tries hash inputs until
+the desired output is achieved.
+
+Throws an exception after 150000 iterations."
+ ;; (display (format #f "suffix is now: ~s\n" suffix))
+ (when (> suffix 1000000000)
+ (raise-exception
+ (make-exception-with-message
+ (format #f "Exceeded maximum attempts for key ~s!" prefix))))
+ (let ((output (get-hash prefix (number->string suffix))))
+ (if (starts-with? output output-prefix)
+ suffix
+ (tiss prefix output-prefix (1+ suffix)))))
+
+(define (get-hash prefix suffix)
+ "Computes the MD5 hash of prefix
+combined with suffix."
+ (md5->string
+ (md5 (string->utf8
+ (string-append prefix suffix)))))
+
+(define (starts-with? str prefix)
+ "Returns #t if STR starts with PREFIX."
+ (let ((prefix-length (string-length prefix)))
+ (and (>= (string-length str) prefix-length)
+ (equal? (substring str 0 prefix-length) prefix))))