Skip to content

Commit 423e306

Browse files
committed
New squarepine_cryptography module!
This is a place to house all sorts of related cryptography stuff, and hashing, CRC, etc... I've also patched up the module versions and licenses, and patched up some docs here and there.
1 parent 79f15e7 commit 423e306

File tree

21 files changed

+228
-35
lines changed

21 files changed

+228
-35
lines changed

modules/squarepine_animation/squarepine_animation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
66
ID: squarepine_animation
77
vendor: SquarePine
8-
version: 0.0.1
8+
version: 1.5.0
99
name: SquarePine Animation
1010
description: A grouping of reusable classes for controlling and managing animations.
1111
website: https://www.squarepine.io
12-
license: Beerware
12+
license: GPLv3
1313
minimumCppStandard: 17
1414
dependencies: squarepine_graphics
1515

modules/squarepine_audio/squarepine_audio.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
66
ID: squarepine_audio
77
vendor: SquarePine
8-
version: 0.0.1
8+
version: 1.5.0
99
name: SquarePine Audio
1010
description: A great backbone for any typical audio project.
1111
website: https://www.squarepine.io
12-
license: Beerware
12+
license: GPLv3
1313
minimumCppStandard: 20
14-
dependencies: squarepine_core juce_dsp
14+
dependencies: squarepine_cryptography juce_dsp
1515
1616
END_JUCE_MODULE_DECLARATION
1717
*/

modules/squarepine_core/squarepine_core.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ namespace sp
4141
#endif
4242
}
4343

44-
#include "cryptography/CRC.cpp"
45-
//#include "cryptography/SHA1.cpp"
4644
#include "debugging/CrashStackTracer.cpp"
4745
#include "misc/ArrayIterationUnroller.cpp"
4846
#include "misc/CodeBeautifiers.cpp"
@@ -66,8 +64,6 @@ namespace sp
6664
#include "networking/User.cpp"
6765
#include "networking/WebServiceUtilities.cpp"
6866
#include "networking/WooCommerce.cpp"
69-
#include "rng/ISAAC.cpp"
70-
#include "rng/Xorshift.cpp"
7167
#include "text/LanguageCodes.cpp"
7268
#include "text/CountryCodes.cpp"
7369
#include "text/LanguageHandler.cpp"

modules/squarepine_core/squarepine_core.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
66
ID: squarepine_core
77
vendor: SquarePine
8-
version: 0.1.0
8+
version: 1.5.0
99
name: SquarePine Core
1010
description: A decent backbone for any project.
1111
website: https://www.squarepine.io
12-
license: Beerware
12+
license: GPLv3
1313
minimumCppStandard: 20
1414
dependencies: juce_audio_utils juce_cryptography juce_opengl
1515
OSXFrameworks: SystemConfiguration
@@ -232,7 +232,6 @@
232232

233233
//==============================================================================
234234
#include "valuetree/VariantConverters.h"
235-
#include "rng/Hashing.h"
236235

237236
//==============================================================================
238237
namespace sp
@@ -259,9 +258,6 @@ namespace sp
259258
#include "behaviours/DeletionNotifier.h"
260259
#include "behaviours/Identifiable.h"
261260
#include "behaviours/Lockable.h"
262-
#include "cryptography/CRC.h"
263-
//#include "cryptography/SHA1.h"
264-
//#include "cryptography/SHA2.h"
265261
#include "debugging/CrashStackTracer.h"
266262
#include "maths/Algebra.h"
267263
#include "maths/Interpolation.h"
@@ -295,9 +291,6 @@ namespace sp
295291
#include "networking/Utilities.h"
296292
#include "networking/WebServiceUtilities.h"
297293
#include "networking/WooCommerce.h"
298-
#include "rng/BlumBlumShub.h"
299-
#include "rng/ISAAC.h"
300-
#include "rng/Xorshift.h"
301294
#include "text/LanguageCodes.h"
302295
#include "text/CountryCodes.h"
303296
#include "text/LanguageHandler.h"
File renamed without changes.
File renamed without changes.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/** FNVHash class representing the FNV-0, FNV-1, and FNV-1a hashes.
2+
Use one of these for setting up hashing of unsigned 32-bit and 64-bit values.
3+
4+
Create one of these with a block of source data or a stream,
5+
and it calculates the FNV hash of that data.
6+
7+
@see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
8+
@see http://www.isthe.com/chongo/tech/comp/fnv/index.html
9+
*/
10+
template<typename Type, Type primeValueSource, Type offsetBasisSource, bool use1AAlgorithm>
11+
class FNVHash final
12+
{
13+
public:
14+
//==============================================================================
15+
/** Some predetermined prime value as per FNV's specs. */
16+
static inline constexpr Type primeValue = primeValueSource;
17+
/** In FNV-0, this should be 0. Otherwise, this will be some pre-calculated value. */
18+
static inline constexpr Type offsetBasis = offsetBasisSource;
19+
20+
//==============================================================================
21+
/** Creates a null FNVHash object. */
22+
FNVHash() noexcept = default;
23+
24+
/** Creates a copy of another FNVHash. */
25+
FNVHash (const FNVHash&) noexcept = default;
26+
27+
/** */
28+
explicit FNVHash (Type initialState) noexcept :
29+
state (initialState)
30+
{
31+
}
32+
33+
/** Creates a hash for the input from a stream.
34+
35+
This will read up to the given number of bytes
36+
from the stream and produce the hash of that.
37+
38+
If the number of bytes to read is negative,
39+
it'll read until the stream is exhausted.
40+
*/
41+
FNVHash (InputStream& input, int64 numBytesToRead = -1)
42+
{
43+
if (numBytesToRead < 0)
44+
numBytesToRead = std::numeric_limits<int64>::max();
45+
46+
std::array<uint8, 512> tempBuffer;
47+
48+
while (numBytesToRead > 0 && ! input.isExhausted())
49+
{
50+
tempBuffer.fill (0);
51+
const auto bytesRead = input.read (tempBuffer.data(), (int) jmin (numBytesToRead, (int64) tempBuffer.size()));
52+
53+
if (bytesRead <= 0)
54+
break;
55+
56+
numBytesToRead -= bytesRead;
57+
58+
for (auto v : tempBuffer)
59+
process (static_cast<Type> (v)); // Deliberately widened to follow suit with the source C examples.
60+
}
61+
}
62+
63+
/** Creates a hash for a block of binary data. */
64+
explicit FNVHash (const MemoryBlock& data) noexcept :
65+
FNVHash (MemoryInputStream (data, false))
66+
{
67+
}
68+
69+
/** Creates a hash for a block of binary data. */
70+
FNVHash (const void* data, size_t numBytes) noexcept :
71+
FNVHash (MemoryBlock (data, numBytes))
72+
{
73+
}
74+
75+
/** Creates a hash for the contents of a file. */
76+
explicit FNVHash (const File& file)
77+
{
78+
if (FileInputStream fin (file); fin.openedOk())
79+
state = FNVHash (fin).get();
80+
}
81+
82+
/** Creates a hash of the characters in a UTF-8 buffer.
83+
84+
Example:
85+
@code
86+
FNVHash myResults (myString.toUTF8());
87+
@endcode
88+
*/
89+
explicit FNVHash (CharPointer_UTF8 utf8) noexcept :
90+
FNVHash (utf8.getAddress(), utf8.getAddress() != nullptr ? utf8.sizeInBytes() - 1 : 0)
91+
{
92+
}
93+
94+
/** Destructor. */
95+
~FNVHash() noexcept = default;
96+
97+
//==============================================================================
98+
/** Copies another FNVHash. */
99+
FNVHash& operator= (const FNVHash&) noexcept = default;
100+
101+
//==============================================================================
102+
/** */
103+
Type process (Type value) noexcept
104+
{
105+
if constexpr (use1AAlgorithm)
106+
{
107+
// FNV-1a:
108+
state ^= value;
109+
state *= primeValue;
110+
}
111+
else
112+
{
113+
// FNV-0 and FNV-1:
114+
state *= primeValue;
115+
state ^= value;
116+
}
117+
118+
return state;
119+
}
120+
121+
/** @returns */
122+
constexpr Type get() const noexcept { return state; }
123+
124+
/** @returns */
125+
String toHexString() const { return String::toHexString (state); }
126+
127+
//==============================================================================
128+
/** @returns */
129+
bool operator== (const FNVHash& other) const noexcept { return state == other.state; }
130+
/** @returns */
131+
bool operator!= (const FNVHash& other) const noexcept { return ! operator== (other); }
132+
133+
private:
134+
//==============================================================================
135+
Type state = offsetBasis;
136+
137+
//==============================================================================
138+
JUCE_LEAK_DETECTOR (FNVHash)
139+
};
140+
141+
//==============================================================================
142+
/** An FNV0 implementation for 32-bit values. */
143+
using FNV0Hash32 = FNVHash<uint32, 0x811c9dc5, 0, false>;
144+
/** An FNV1 implementation for 32-bit values. */
145+
using FNV1Hash32 = FNVHash<uint32, 0x811c9dc5, 0x811c9dc5, false>;
146+
/** An FNV1a implementation for 32-bit values. */
147+
using FNV1aHash32 = FNVHash<uint32, 0x811c9dc5, 0x811c9dc5, true>;
148+
149+
/** An FNV0 implementation for 64-bit values. */
150+
using FNV0Hash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0ULL, false>;
151+
/** An FNV1 implementation for 64-bit values. */
152+
using FNV1Hash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0xcbf29ce484222325ULL, false>;
153+
/** An FNV1a implementation for 64-bit values. */
154+
using FNV1aHash64 = FNVHash<uint64, 0x00000100000001b3ULL, 0xcbf29ce484222325ULL, true>;
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)