# sha256 function

**URL:** <https://discourse.gohugo.io/t/sha256-function/4665>\
**Category:** feature\
**Created:** [November 26, 2016, 2:01pm UTC](https://discourse.gohugo.io/t/sha256-function/4665 "2016-11-26T14:01:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![36928495](https://avatars.discourse-cdn.com/v4/letter/3/e68b1a/32.png) [@36928495](https://discourse.gohugo.io/u/36928495)\
**Post date:** [November 26, 2016, 2:01pm UTC](https://discourse.gohugo.io/t/sha256-function/4665/1 "2016-11-26T14:01:12Z")

</div>

Currently their are the [md5](https://gohugo.io/templates/functions/#md5) and [sha1](https://gohugo.io/templates/functions/#sha1) functions. It would be great if SHA256 were also supported.

Because then combined with the [base64](https://gohugo.io/templates/functions/#base64encode-and-base64decode) and [readFile](https://gohugo.io/templates/functions/#readfile) functions, the [integrity html attribute](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) could be used.

Example:

`<script src="/behaviour.js" integrity="sha256-{{ (sha256 (readFile "static/behaviour.js")) | base64Encode }}"></script>`

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [November 26, 2016, 6:11pm UTC](https://discourse.gohugo.io/t/sha256-function/4665/2 "2016-11-26T18:11:24Z")

</div>

A sha256 func pull request is welcomed – but note that without any caching, adding that script definition to a partial that is included in every template will be veeeery heavy in the performance department.

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [December 4, 2016, 3:05pm UTC](https://discourse.gohugo.io/t/sha256-function/4665/3 "2016-12-04T15:05:15Z")

</div>

I submitted a pull request that is already merged.

> <https://github.com/gohugoio/hugo/pull/2762>

---

<div class="post-metadata">

**Author:** ![36928495](https://avatars.discourse-cdn.com/v4/letter/3/e68b1a/32.png) [@36928495](https://discourse.gohugo.io/u/36928495)\
**Post date:** [December 21, 2016, 1:28pm UTC](https://discourse.gohugo.io/t/sha256-function/4665/4 "2016-12-21T13:28:41Z")

</div>

To anyone who has searched for the integrity attribute:

Note that the above example will work, but won’t be valid.  
The sha256 function outputs a hexadecimal value, but what is needed, is binary.

The documentation clearly states that the return value will be a checksum, instead of a digest. Excuse the noise.

---

<div class="post-metadata">

**Author:** ![carlmjohnson](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@carlmjohnson](https://discourse.gohugo.io/u/carlmjohnson)\
**Post date:** [April 27, 2017, 10:05pm UTC](https://discourse.gohugo.io/t/sha256-function/4665/5 "2017-04-27T22:05:20Z")

</div>

Interesting. I think a better approach is to write a tool that creates an integrity-manifest.json and puts it into the /data/ directory so that Hugo can access it that way. It prevents you’re needing to recalculate for every page, as BEP noted is a drawback of having it in the template.
