Created
December 26, 2015 00:04
-
-
Save vfalconi/e2b3a61cd93a4c2bd520 to your computer and use it in GitHub Desktop.
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 | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
if ( ! function_exists('file_digest')) | |
{ | |
/** | |
* File digest | |
* | |
* Creates a base64-encoded hash of a file for a subresource integrity | |
* check | |
* | |
* @param string | |
* @param string | |
* @return string | |
*/ | |
function file_digest($file, $hash_function = 'sha256') | |
{ | |
$file_contents = file_get_contents($file); | |
$hash = hash($hash_function, $file_contents, true); | |
$encoded_hash = base64_encode($hash); | |
return $hash_function.'-'.$encoded_hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment