Skip to content

Instantly share code, notes, and snippets.

@roman-neuhauser
Created March 2, 2014 10:38
Show Gist options
  • Save roman-neuhauser/9304735 to your computer and use it in GitHub Desktop.
Save roman-neuhauser/9304735 to your computer and use it in GitHub Desktop.
crypto = require 'crypto'
fs = require 'fs'
path = require 'path'
digest = crypto.createHash 'sha1'
str = 'hello world!'
digest.update str
hex = digest.digest 'hex'
segs = (hex) ->
rv = hex[i..i+1] for _, i in hex.substr 0, 20 by 2
mkseg = (parts, i, done) ->
if i >= parts.length
return do done
fs.mkdir (path.join parts[0..i]...), (err) ->
return done err if err and err.code isnt 'EEXIST'
mkseg parts, i+1, done
mkpath = (parts, done) ->
mkseg parts, 0, done
store = (payload, hex, done) ->
parts = segs hex
mkpath parts, (err, args...) ->
return done err if err
fpath = path.join parts..., hex
fs.writeFile fpath, payload, (err, args...) ->
done err, args...
load = (hex, done) ->
parts = segs hex
fpath = path.join parts..., hex
fs.readFile fpath, 'utf8', done
store str, hex, (args...) ->
console.dir args
load hex, (args...) ->
console.dir args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment