summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2018-12-11 23:21:42 +0100
committertv <tv@krebsco.de>2018-12-11 23:21:42 +0100
commit474e3e2e4513a5d2df89789885725b176e7ec532 (patch)
treecf3f87a5de3d2dc68958ab45da50bdb7e87d76a7 /lib
parente55b54092803dbddbafe4971c9c7da4b5679988d (diff)
lib: import generally useful stuff from genipv6
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix27
-rw-r--r--lib/krebs/genipv6.nix32
2 files changed, 31 insertions, 28 deletions
diff --git a/lib/default.nix b/lib/default.nix
index e352c7be9..64b2d48ab 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -112,6 +112,33 @@ let
(if test ".*::.*" a
then a
else group-zeros (drop-leading-zeros a));
+
+ hashToLength = n: s: substring 0 n (hashString "sha256" s);
+
+ dropLast = n: xs: reverseList (drop n (reverseList xs));
+ takeLast = n: xs: reverseList (take n (reverseList xs));
+
+ # Split string into list of chunks where each chunk is at most n chars long.
+ # The leftmost chunk might shorter.
+ # Example: stringToGroupsOf "123456" -> ["12" "3456"]
+ stringToGroupsOf = n: s: let
+ acc =
+ foldl'
+ (acc: c: if stringLength acc.chunk < n then {
+ chunk = acc.chunk + c;
+ chunks = acc.chunks;
+ } else {
+ chunk = c;
+ chunks = acc.chunks ++ [acc.chunk];
+ })
+ {
+ chunk = "";
+ chunks = [];
+ }
+ (stringToCharacters s);
+ in
+ filter (x: x != []) ([acc.chunk] ++ acc.chunks);
+
};
in
diff --git a/lib/krebs/genipv6.nix b/lib/krebs/genipv6.nix
index bf3ebab38..af1df6d0e 100644
--- a/lib/krebs/genipv6.nix
+++ b/lib/krebs/genipv6.nix
@@ -26,7 +26,7 @@ let {
inherit subnetname;
subnetCIDR = "${subnetAddress}/${toString subnetPrefixLength}";
subnetAddress = appendZeros subnetPrefixLength subnetPrefix;
- subnetHash = hash 4 subnetname;
+ subnetHash = hashToLength 4 subnetname;
subnetPrefix = joinAddress netPrefix subnetHash;
subnetPrefixLength = netPrefixLength + 16;
@@ -34,7 +34,9 @@ let {
set =
concatStringsSep
":"
- (stringToGroupsOf 4 (hash (suffixLength / 4) suffixSpec.hostName));
+ (stringToGroupsOf
+ 4
+ (hashToLength (suffixLength / 4) suffixSpec.hostName));
string = suffixSpec;
};
suffixLength = addressLength - subnetPrefixLength;
@@ -54,32 +56,6 @@ let {
in
formatAddress (map (const "0") (range 1 zeroCount) ++ parsedaddr);
- # Split string into list of chunks where each chunk is at most n chars long.
- # The leftmost chunk might shorter.
- # Example: stringToGroupsOf "123456" -> ["12" "3456"]
- stringToGroupsOf = n: s: let
- acc =
- foldl'
- (acc: c: if stringLength acc.chunk < n then {
- chunk = acc.chunk + c;
- chunks = acc.chunks;
- } else {
- chunk = c;
- chunks = acc.chunks ++ [acc.chunk];
- })
- {
- chunk = "";
- chunks = [];
- }
- (stringToCharacters s);
- in
- filter (x: x != []) ([acc.chunk] ++ acc.chunks);
-
- hash = n: s: substring 0 n (hashString "sha256" s);
-
- dropLast = n: xs: reverseList (drop n (reverseList xs));
- takeLast = n: xs: reverseList (take n (reverseList xs));
-
hasEmptyPrefix = xs: take 2 xs == ["" ""];
hasEmptySuffix = xs: takeLast 2 xs == ["" ""];
hasEmptyInfix = xs: any (x: x == "") (trimEmpty 2 xs);