-
-
Save it-can/0c13e569d3eb06c44540bf91369f18d8 to your computer and use it in GitHub Desktop.
SRI-poly:: Adapted from http://tenzer.dk/generating-subresource-integrity-checksums/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sri | |
import ( | |
"crypto/sha256" | |
"encoding/base64" | |
"fmt" | |
"io/ioutil" | |
) | |
func Generate256(file string) (string, error) { | |
body, err := ioutil.ReadFile(file) | |
if err != nil { | |
return "", err | |
} | |
hash := sha256.Sum256(body) | |
sha := base64.StdEncoding.EncodeToString(hash[:]) | |
return fmt.Sprintf("sha256-%s", sha), nil | |
} | |
// package main | |
// | |
// import ( | |
// "fmt" | |
// "sri" | |
// ) | |
// | |
// func main() { | |
// sha, err := sri.Generate256("./src/sri.go") | |
// | |
// if err != nil { | |
// panic(err) | |
// } | |
// | |
// fmt.Println(sha) | |
// } | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); | |
var fs = require('fs'); | |
module.exports = function (file) { | |
var enc = 'utf8'; | |
var body = fs.readFileSync(file, { encoding: enc }); | |
var hash = crypto.createHash('sha256').update(body, enc); | |
var sha = hash.digest('base64'); | |
return 'sha256-' + sha; | |
} | |
// > var generate256 = require('./sri.js'); | |
// undefined | |
// > generate256("./sri.js"); | |
// 'sha256-U+9zwgl5Wcs2X0IzBnN/4Ri016f8lkdtvmPZuIytt8Y=' | |
// > | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function generate256($path) { | |
$file = fopen($path, "r"); | |
$body = fread($file, filesize($path)); | |
$hash = hash('sha256', $body, true); | |
$sha = base64_encode($hash); | |
return "sha256-$sha"; | |
} | |
// php > include './sri.php'; | |
// php > echo generate256("./sri.php"); | |
// sha256-wdToXd37ShQdyh8ljZAJoqvPXdQEiyqGv/IBnbKkg3A= | |
// php > | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hashlib | |
def generate256(file): | |
with open(file, 'r') as f: | |
body = f.read() | |
hash = hashlib.sha256(body).digest() | |
sha = base64.b64encode(hash).decode() | |
return 'sha256-{}'.format(sha) | |
# Python 2.7.10 (default, Oct 14 2015, 16:09:02) | |
# [GCC 5.2.1 20151010] on linux2 | |
# Type "help", "copyright", "credits" or "license" for more information. | |
# >>> from sri import generate256 | |
# >>> generate256("sri.py") | |
# 'sha256-ZDTvTHvqQDypoPxKGrkYPNVeXQPvKqnZeAFvRZ1l+ik=' | |
# >>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
require 'base64' | |
def generate256(file) | |
body = File.read(file) | |
hash = Digest::SHA256.digest(body) | |
sha = Base64.encode64(hash).strip | |
"sha256-#{sha}" | |
end | |
# irb(main):001:0> require './sri' | |
# => true | |
# irb(main):002:0> generate256("sri.rb") | |
# => "sha256-zna8a/EB5hMXmiyjg+iSy5/Xar/+Cv0EDF1STWr+Mwo=" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generate256 { | |
echo "sha256-`cat $1 | openssl dgst -sha256 -binary | openssl enc -base64 -A`" | |
} | |
# jmervine@laptop:~/sri $ source sri.sh | |
# jmervine@laptop:~/sri $ generate256 "sri.sh" | |
# sha256-wS2IFRvHJO0XSkrEvudmfw2nBnNz+E1o4AJ2Cf/HpZs= | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment