Skip to content

Commit 0486091

Browse files
authored
Merge pull request #1288 from jcpunk/crc32
2 parents a5ef0c9 + afb2e99 commit 0486091

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed

REFERENCE.md

+69-4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ the provided regular expression.
173173
* [`sort`](#sort): Sorts strings and arrays lexically.
174174
* [`sprintf_hash`](#sprintf_hash): Uses sprintf with named references.
175175
* [`squeeze`](#squeeze): Returns a new string where runs of the same character that occur in this set are replaced by a single character.
176+
* [`stdlib::crc32`](#stdlibcrc32): Run a CRC32 calculation against a given value.
176177
* [`stdlib::deferrable_epp`](#stdlibdeferrable_epp): This function returns either a rendered template or a deferred function to render at runtime. If any of the values in the variables hash are
177178
* [`stdlib::end_with`](#stdlibend_with): Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
178179
* [`stdlib::ensure`](#stdlibensure): function to cast ensure parameter to resource specific value
@@ -4190,7 +4191,9 @@ hash types are:
41904191
|bcrypt-x |2x |bug compatible |
41914192
|bcrypt-y |2y |historic alias for 2b|
41924193

4193-
The third argument to this function is the salt to use.
4194+
The third argument to this function is the salt to use. For bcrypt-type hashes,
4195+
the first two characters of the salt represent a strength parameter, with a value
4196+
between 4 and 31 inclusive.
41944197

41954198
> *Note:*: this uses the Puppet Server's implementation of crypt(3). If your
41964199
environment contains several different operating systems, ensure that they
@@ -4215,7 +4218,9 @@ hash types are:
42154218
|bcrypt-x |2x |bug compatible |
42164219
|bcrypt-y |2y |historic alias for 2b|
42174220

4218-
The third argument to this function is the salt to use.
4221+
The third argument to this function is the salt to use. For bcrypt-type hashes,
4222+
the first two characters of the salt represent a strength parameter, with a value
4223+
between 4 and 31 inclusive.
42194224

42204225
> *Note:*: this uses the Puppet Server's implementation of crypt(3). If your
42214226
environment contains several different operating systems, ensure that they
@@ -4662,6 +4667,66 @@ The squeeze function.
46624667

46634668
Returns: `Any` a new string where runs of the same character that occur in this set are replaced by a single character.
46644669

4670+
### <a name="stdlibcrc32"></a>`stdlib::crc32`
4671+
4672+
Type: Ruby 4.x API
4673+
4674+
Run a CRC32 calculation against a given value.
4675+
4676+
#### Examples
4677+
4678+
##### Check a simple string value
4679+
4680+
```puppet
4681+
stdlib::crc32('my string') == '18fbd270'
4682+
```
4683+
4684+
##### Check a Sensitive datatype
4685+
4686+
```puppet
4687+
stdlib::crc32(sensitive('my string')) == '18fbd270'
4688+
```
4689+
4690+
##### Check a number
4691+
4692+
```puppet
4693+
stdlib::crc32(100.0) == 'a3fd429a'
4694+
stdlib::crc32(100.00000) == 'a3fd429a'
4695+
```
4696+
4697+
#### `stdlib::crc32(Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]] $my_data)`
4698+
4699+
Run a CRC32 calculation against a given value.
4700+
4701+
Returns: `String` String
4702+
4703+
##### Examples
4704+
4705+
###### Check a simple string value
4706+
4707+
```puppet
4708+
stdlib::crc32('my string') == '18fbd270'
4709+
```
4710+
4711+
###### Check a Sensitive datatype
4712+
4713+
```puppet
4714+
stdlib::crc32(sensitive('my string')) == '18fbd270'
4715+
```
4716+
4717+
###### Check a number
4718+
4719+
```puppet
4720+
stdlib::crc32(100.0) == 'a3fd429a'
4721+
stdlib::crc32(100.00000) == 'a3fd429a'
4722+
```
4723+
4724+
##### `my_data`
4725+
4726+
Data type: `Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]]`
4727+
4728+
The ScalarData to evaluate
4729+
46654730
### <a name="stdlibdeferrable_epp"></a>`stdlib::deferrable_epp`
46664731

46674732
Type: Puppet Language
@@ -4746,7 +4811,7 @@ Type: Puppet Language
47464811

47474812
function to cast ensure parameter to resource specific value
47484813

4749-
#### `stdlib::ensure(Variant[Boolean, Enum['present', 'absent']] $ensure, Enum['directory', 'link', 'mounted', 'service', 'file', 'package'] $resource)`
4814+
#### `stdlib::ensure(Variant[Boolean, Enum['present', 'absent']] $ensure, Optional[Enum['directory', 'link', 'mounted', 'service', 'file', 'package']] $resource = undef)`
47504815

47514816
The stdlib::ensure function.
47524817

@@ -4760,7 +4825,7 @@ Data type: `Variant[Boolean, Enum['present', 'absent']]`
47604825

47614826
##### `resource`
47624827

4763-
Data type: `Enum['directory', 'link', 'mounted', 'service', 'file', 'package']`
4828+
Data type: `Optional[Enum['directory', 'link', 'mounted', 'service', 'file', 'package']]`
47644829

47654830

47664831

lib/puppet/functions/stdlib/crc32.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'zlib'
4+
# @note
5+
# The CRC32 algorithm can easily generate collisions,
6+
# but may be useful for generating sharding, describing
7+
# secrets, or seeding nonce values.
8+
#
9+
# @summary
10+
# Run a CRC32 calculation against a given value.
11+
Puppet::Functions.create_function(:'stdlib::crc32') do
12+
# @param my_data The ScalarData to evaluate
13+
# @example Check a simple string value
14+
# stdlib::crc32('my string') == '18fbd270'
15+
# @example Check a Sensitive datatype
16+
# stdlib::crc32(sensitive('my string')) == '18fbd270'
17+
# @example Check a number
18+
# stdlib::crc32(100.0) == 'a3fd429a'
19+
# stdlib::crc32(100.00000) == 'a3fd429a'
20+
# @return String
21+
dispatch :crc32 do
22+
param 'Variant[ScalarData, Sensitive[ScalarData], Binary, Sensitive[Binary]]', :my_data
23+
return_type 'String'
24+
end
25+
26+
def crc32(my_data)
27+
Zlib.crc32(my_data.unwrap.to_s).to_s(16).downcase
28+
rescue
29+
Zlib.crc32(my_data.to_s).to_s(16).downcase
30+
end
31+
end

spec/functions/crc32_spec.rb

+44
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::crc32' 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::crc32}) }
9+
end
10+
11+
context 'when testing a simple string' do
12+
it { is_expected.to run.with_params('abc').and_return('352441c2') }
13+
it { is_expected.to run.with_params('acb').and_return('5b384015') }
14+
it { is_expected.to run.with_params('my string').and_return('18fbd270') }
15+
it { is_expected.to run.with_params('0').and_return('f4dbdf21') }
16+
end
17+
18+
context 'when testing a sensitive string' do
19+
it { is_expected.to run.with_params(sensitive('my string')).and_return('18fbd270') }
20+
end
21+
22+
context 'when testing an integer' do
23+
it { is_expected.to run.with_params(0).and_return('f4dbdf21') }
24+
it { is_expected.to run.with_params(100).and_return('237750ea') }
25+
it { is_expected.to run.with_params(sensitive(100)).and_return('237750ea') }
26+
end
27+
28+
context 'when testing a float' do
29+
it { is_expected.to run.with_params(200.3).and_return('7d5469f0') }
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('a3fd429a') }
33+
it { is_expected.to run.with_params(sensitive(100.0000)).and_return('a3fd429a') }
34+
end
35+
36+
context 'when testing a bool' do
37+
it { is_expected.to run.with_params(true).and_return('fdfc4c8d') }
38+
it { is_expected.to run.with_params(false).and_return('2bcd6830') }
39+
end
40+
41+
context 'when testing a binary' do
42+
it { is_expected.to run.with_params("\xFE\xED\xBE\xEF").and_return('ac3481a4') }
43+
end
44+
end

0 commit comments

Comments
 (0)