Skip to content

Commit 41c6aef

Browse files
authored
Merge pull request #1289 from jcpunk/sha256
Add `stdlib::sha256`
2 parents 0486091 + ec1f779 commit 41c6aef

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

REFERENCE.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ the provided regular expression.
180180
* [`stdlib::extname`](#stdlibextname): Returns the Extension (the Portion of Filename in Path starting from the
181181
last Period).
182182
* [`stdlib::ip_in_range`](#stdlibip_in_range): Returns true if the ipaddress is within the given CIDRs
183+
* [`stdlib::sha256`](#stdlibsha256): Run a SHA256 calculation against a given value.
183184
* [`stdlib::start_with`](#stdlibstart_with): Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
184185
* [`stdlib::str2resource`](#stdlibstr2resource): This converts a string to a puppet resource.
185186
* [`stdlib::xml_encode`](#stdlibxml_encode): Encode strings for XML files
@@ -4918,6 +4919,66 @@ Data type: `Variant[String, Array]`
49184919
One CIDR or an array of CIDRs
49194920
defining the range(s) to check against
49204921

4922+
### <a name="stdlibsha256"></a>`stdlib::sha256`
4923+
4924+
Type: Ruby 4.x API
4925+
4926+
Run a SHA256 calculation against a given value.
4927+
4928+
#### Examples
4929+
4930+
##### Check a simple string value
4931+
4932+
```puppet
4933+
stdlib::sha256('my string') == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
4934+
```
4935+
4936+
##### Check a Sensitive datatype
4937+
4938+
```puppet
4939+
stdlib::sha256(sensitive('my string')) == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
4940+
```
4941+
4942+
##### Check a number
4943+
4944+
```puppet
4945+
stdlib::sha256(100.0) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
4946+
stdlib::sha256(100.00000) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
4947+
```
4948+
4949+
#### `stdlib::sha256(Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]] $my_data)`
4950+
4951+
Run a SHA256 calculation against a given value.
4952+
4953+
Returns: `String` String
4954+
4955+
##### Examples
4956+
4957+
###### Check a simple string value
4958+
4959+
```puppet
4960+
stdlib::sha256('my string') == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
4961+
```
4962+
4963+
###### Check a Sensitive datatype
4964+
4965+
```puppet
4966+
stdlib::sha256(sensitive('my string')) == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
4967+
```
4968+
4969+
###### Check a number
4970+
4971+
```puppet
4972+
stdlib::sha256(100.0) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
4973+
stdlib::sha256(100.00000) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
4974+
```
4975+
4976+
##### `my_data`
4977+
4978+
Data type: `Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]]`
4979+
4980+
The ScalarData to evaluate
4981+
49214982
### <a name="stdlibstart_with"></a>`stdlib::start_with`
49224983

49234984
Type: Ruby 4.x API

lib/puppet/functions/stdlib/sha256.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'digest'
4+
# @summary
5+
# Run a SHA256 calculation against a given value.
6+
Puppet::Functions.create_function(:'stdlib::sha256') do
7+
# @param my_data The ScalarData to evaluate
8+
# @example Check a simple string value
9+
# stdlib::sha256('my string') == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
10+
# @example Check a Sensitive datatype
11+
# stdlib::sha256(sensitive('my string')) == '2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5'
12+
# @example Check a number
13+
# stdlib::sha256(100.0) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
14+
# stdlib::sha256(100.00000) == '43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0'
15+
# @return String
16+
dispatch :sha256 do
17+
param 'Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]]', :my_data
18+
return_type 'String'
19+
end
20+
21+
def sha256(my_data)
22+
Digest::SHA256.hexdigest(my_data.unwrap.to_s)
23+
rescue
24+
Digest::SHA256.hexdigest(my_data.to_s)
25+
end
26+
end

spec/functions/sha256_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'stdlib::sha256' do
6+
context 'when default' do
7+
it { is_expected.not_to eq(nil) }
8+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{stdlib::sha256}) }
9+
end
10+
11+
context 'when testing a simple string' do
12+
it { is_expected.to run.with_params('abc').and_return('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') }
13+
it { is_expected.to run.with_params('acb').and_return('8e9766083b3bfc2003f791c9853941b0ea035d16379bfec16b72d376e272fa57') }
14+
it { is_expected.to run.with_params('my string').and_return('2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5') }
15+
it { is_expected.to run.with_params('0').and_return('5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9') }
16+
end
17+
18+
context 'when testing a sensitive string' do
19+
it { is_expected.to run.with_params(sensitive('my string')).and_return('2f7e2089add0288a309abd71ffcc3b3567e2d4215e20e6ed3b74d6042f7ef8e5') }
20+
end
21+
22+
context 'when testing an integer' do
23+
it { is_expected.to run.with_params(0).and_return('5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9') }
24+
it { is_expected.to run.with_params(100).and_return('ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306') }
25+
it { is_expected.to run.with_params(sensitive(100)).and_return('ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306') }
26+
end
27+
28+
context 'when testing a float' do
29+
it { is_expected.to run.with_params(200.3).and_return('441adfa0dd670f4193e4b6e4e373bd7fd3861ee53c834c562b825af79bf7dc98') }
30+
31+
# .0 isn't always converted into an integer, but should have rational truncation
32+
it { is_expected.to run.with_params(100.0).and_return('43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0') }
33+
it { is_expected.to run.with_params(sensitive(100.0000)).and_return('43b87f618caab482ebe4976c92bcd6ad308b48055f1c27b4c574f3e31d7683e0') }
34+
end
35+
36+
context 'when testing a bool' do
37+
it { is_expected.to run.with_params(true).and_return('b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b') }
38+
it { is_expected.to run.with_params(false).and_return('fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa') }
39+
end
40+
41+
context 'when testing a binary' do
42+
it { is_expected.to run.with_params("\xFE\xED\xBE\xEF").and_return('bf6b255a261ddde9ea66060dcb239e06d321ad37755d2a97a5846f5144b779b4') }
43+
end
44+
end

0 commit comments

Comments
 (0)