| 1 |
--- |
| 2 |
title: resources.Fingerprint |
| 3 |
description: Cryptographically hashes the content of the given resource. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [fingerprint] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: ['resources.Fingerprint [ALGORITHM] RESOURCE'] |
| 11 |
--- |
| 12 |
|
| 13 |
```go-html-template |
| 14 |
{{ with resources.Get "js/main.js" }} |
| 15 |
{{ with . | fingerprint "sha256" }} |
| 16 |
<script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script> |
| 17 |
{{ end }} |
| 18 |
{{ end }} |
| 19 |
``` |
| 20 |
|
| 21 |
Hugo renders this to something like: |
| 22 |
|
| 23 |
```html |
| 24 |
<script src="/js/main.62e...df1.js" integrity="sha256-Yuh...rfE=" crossorigin="anonymous"></script> |
| 25 |
``` |
| 26 |
|
| 27 |
Although most commonly used with CSS and JavaScript resources, you can use the `resources.Fingerprint` function with any resource type. |
| 28 |
|
| 29 |
The hash algorithm may be one of `md5`, `sha256` (default), `sha384`, or `sha512`. |
| 30 |
|
| 31 |
After cryptographically hashing the resource content: |
| 32 |
|
| 33 |
1. The values returned by the `.Permalink` and `.RelPermalink` methods include the hash sum |
| 34 |
1. The resource's `.Data.Integrity` method returns a [Subresource Integrity][] (SRI) value consisting of the name of the hash algorithm, one hyphen, and the base64-encoded hash sum |
| 35 |
|
| 36 |
[Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity |