Skip to content

Instantly share code, notes, and snippets.

@vfalconi
Created December 26, 2015 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfalconi/e2b3a61cd93a4c2bd520 to your computer and use it in GitHub Desktop.
Save vfalconi/e2b3a61cd93a4c2bd520 to your computer and use it in GitHub Desktop.
<?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