Skip to content

Instantly share code, notes, and snippets.

@firien
Last active August 29, 2015 14:02
Show Gist options
  • Save firien/64b221cf9bab55decc88 to your computer and use it in GitHub Desktop.
Save firien/64b221cf9bab55decc88 to your computer and use it in GitHub Desktop.
example inline sinatra file
require 'sinatra'
require 'coffee_script'
require 'haml'
# configure do
# set :bind, '0.0.0.0'
# end
js = DATA.read
get '/shibboleth.js' do
coffee js
end
get '/' do
haml %{!!!
%html
%head
%script{src: '/shibboleth.js'}
%meta{content: 'initial-scale=1.0, maximum-scale=1.0', name: 'viewport'}
:css
body {
font-size: 1.5em;
}
textarea {
font-size: 1rem;
width: 100%
}
#results {
overflow-y:scroll;
}
%body
%textarea eµ
%button SHA256
#results
}
end
__END__
#return Uint8Array
to_array = (str) ->
new Uint8Array encodeURIComponent(str).match(/%[A-F0-9]{2}|./gi).map (c) ->
if /^%/.test c
parseInt(c.substr(1), 16)
else
c.charCodeAt(0)
to_hex = (digestValue) ->
digestValue = new Uint8Array digestValue
hex = ""
i = 0
len = digestValue.length
while i < len
zeropad = if (digestValue[i] < 0x10) then "0" else ""
hex += zeropad + digestValue[i].toString(16)
i++
return hex
document.addEventListener('DOMContentLoaded', ->
_crypto = window.msCrypto || window.crypto
subtle = _crypto.subtle || _crypto.webkitSubtle
show = (buf) ->
document.querySelector('#results').textContent = to_hex buf
document.querySelector('button').addEventListener('click', ->
digestOp = subtle.digest(name: "SHA-256", to_array(document.querySelector('textarea').value))
if Promise? && digestOp instanceof Promise
digestOp.then (result) -> show result
else
digestOp.oncomplete = (evt) -> show evt.target.result
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment