Answers.Body: <p>Hope this helps:</p>
  <p>I needed a hash that only changed if the resource file is modified thus allowing reliable caching.</p>
  <p>Usage:
  &lt;img src=&quot;{{ hash('/images/my_image.jpg') }}&quot;</p>
  <p>Can be used for javascript, css or an image resource.</p>
  <pre>
  // src/Twig/appExtension.php
  namespace App\Twig;
  
  use Twig\Extension\AbstractExtension;
  use Twig\TwigFunction;
  
  class AppExtension extends AbstractExtension
  {
      public function getFunctions()
      {
          return [
              new TwigFunction('hash', [$this, 'hash'])
          ];
      }
  
      // add hash to resource
      public function hash($filename)
      {
          $filepath = getcwd() . $filename;
          $md5 = md5_file($filepath);
          return $filename . "?hash=" . $md5;
      }
  }
  </pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-20T08:15:38.447Z
Answers.LastActivityDate: 2021-04-20T08:24:08.977Z
Answers.LastEditDate: 2021-04-20T08:24:08.977Z
Answers.UserDisplayName: Frankos
Answers.Body: <p>sounds like a simple <a href="https://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a> will do the trick for you. You will need to set up your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up" rel="nofollow">main plugin class</a> and a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">variable class</a>. </p>
  
  <p>Your variable class will look something like this:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class YourPluginNameVariable
  {
      public function makeHash($string)
      {
          $hashed = md5($string);
          return $hashed;
      }
  }
  </code></pre>
  
  <p>You can call this in your templates like this:</p>
  
  <pre><code>{{craft.yourpluginname.makeHash('my phrase here')}}
  </code></pre>
  
  <p>Hope this helps!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-14T19:05:15.430Z
Answers.Text: That's what I thought I might need to do. Just didn't want to reinvent the wheel if I didn't need to. Thank you!
Answers.UserDisplayName: John Morton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-14T19:45:10.740Z
Answers.Text: I wrote one quickly and have posted it to my GitHub page: https://github.com/johnfmorton/Craft-MD5-Twig-Filter-Plugin You use it like {{ "my phrase here" | toMd5 }}
Answers.UserDisplayName: John Morton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-14T19:48:06.137Z
Answers.Text: Nice! Seems a cleaner than using the variable route in my answer.
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-14T18:59:56.720Z
Answers.LastActivityDate: 2015-02-14T18:59:56.720Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>I've been digging around and haven't found an existing Twig filter for what I'm trying to do. Basically, I'd like a filter that would provide a hash of a phrase.</p>
  
  <p>Pseudo-template code would be something like this:</p>
  
  <pre><code>{{ "my phrase here"|hashwithmd5 }} 
  </code></pre>
  
  <p>And would generate this in my rendered template:</p>
  
  <pre><code>6e6d5618f8ab8a302a236eac07c59300
  </code></pre>
  
  <p>Does something like this exist? Am I going about this in a bad way?</p>
  
Tags: <filter><hash>
Title: Craft or Twig filter for MD5 or SHA3 hash creation?
ThreadText: # Craft or Twig filter for MD5 or SHA3 hash creation?
  
  (Asked by: username_0 on 2015-02-14)
  
  I've been digging around and haven't found an existing Twig filter for what I'm trying to do. Basically, I'd like a filter that would provide a hash of a phrase.
  
  Pseudo-template code would be something like this:
  
  ```
  {{ "my phrase here"|hashwithmd5 }} 
  
  ```
  
  And would generate this in my rendered template:
  
  ```
  6e6d5618f8ab8a302a236eac07c59300
  
  ```
  
  Does something like this exist? Am I going about this in a bad way?
  
  **Tags:** filter, hash
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-02-14. Score: 4 | Accepted answer
  
  sounds like a simple [plugin](https://buildwithcraft.com/docs/plugins/introduction) will do the trick for you. You will need to set up your [main plugin class](http://buildwithcraft.com/docs/plugins/setting-things-up) and a [variable class](http://buildwithcraft.com/docs/plugins/variables).
  
  Your variable class will look something like this:
  
  ```
  <?php
  namespace Craft;
  
  class YourPluginNameVariable
  {
      public function makeHash($string)
      {
          $hashed = md5($string);
          return $hashed;
      }
  }
  
  ```
  
  You can call this in your templates like this:
  
  ```
  {{craft.yourpluginname.makeHash('my phrase here')}}
  
  ```
  
  Hope this helps!
  
  > **Comments:**
  > **username_0**: That's what I thought I might need to do. Just didn't want to reinvent the wheel if I didn't need to. Thank you!  
  > **username_0**: I wrote one quickly and have posted it to my GitHub page: https://github.com/johnfmorton/Craft-MD5-Twig-Filter-Plugin You use it like {{ "my phrase here" | toMd5 }}  
  > **username_2**: Nice! Seems a cleaner than using the variable route in my answer.  
  
  ---
  
  ### Answer by username_1 on 2021-04-20. Score: 1
  
  Hope this helps:
  
  I needed a hash that only changed if the resource file is modified thus allowing reliable caching.
  
  Usage:
  <img src="{{ hash('/images/my\_image.jpg') }}"
  
  Can be used for javascript, css or an image resource.
  
  ```
  
  // src/Twig/appExtension.php
  namespace App\Twig;
  
  use Twig\Extension\AbstractExtension;
  use Twig\TwigFunction;
  
  class AppExtension extends AbstractExtension
  {
      public function getFunctions()
      {
          return [
              new TwigFunction('hash', [$this, 'hash'])
          ];
      }
  
      // add hash to resource
      public function hash($filename)
      {
          $filepath = getcwd() . $filename;
          $md5 = md5_file($filepath);
          return $filename . "?hash=" . $md5;
      }
  }
  
  ```
  
  
  ---
  
  