Skip to content

Instantly share code, notes, and snippets.

@owencm
owencm / blob-to-image.js
Created January 12, 2019 18:01
Convert a blob to an image with JavaScript (e.g. to render blob to canvas)
const blobToImage = (blob) => {
return new Promise(resolve => {
const url = URL.createObjectURL(blob)
let img = new Image()
img.onload = () => {
URL.revokeObjectURL(url)
resolve(img)
}
img.src = url
})
@GaspardP
GaspardP / sha256.js
Created June 4, 2015 15:21
SHA-256 with Javascript and Web Crypto
// Computes the SHA-256 digest of a string with Web Crypto
// Source: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
function sha256(str) {
// Get the string as arraybuffer.
var buffer = new TextEncoder("utf-8").encode(str)
return crypto.subtle.digest("SHA-256", buffer).then(function(hash) {
return hex(hash)
})
}