diff --git a/.config/modules.json b/.config/modules.json index 21ef9a0..60221b1 100644 --- a/.config/modules.json +++ b/.config/modules.json @@ -401,6 +401,12 @@ "category": "crypto", "module": "Algorithm_Identifier", "description": "Identify the type of hashing or encryption used in a string" + }, + { + "id": "0067", + "category": "Cloud", + "module": "Account_Identifier", + "description": "Provides the AWS key for extracting the account ID" } ] } \ No newline at end of file diff --git a/lib/Spellbook/Cloud/Account_Identifier.pm b/lib/Spellbook/Cloud/Account_Identifier.pm new file mode 100644 index 0000000..7924a12 --- /dev/null +++ b/lib/Spellbook/Cloud/Account_Identifier.pm @@ -0,0 +1,42 @@ +package Spellbook::Cloud::Account_Identifier { + use strict; + use warnings; + use MIME::Base32 qw(decode_base32); + use Math::BigInt; + + sub new { + my ($self, $parameters) = @_; + my ($help, $key); + + Getopt::Long::GetOptionsFromArray ( + $parameters, + "h|help" => \$help, + "k|key=s" => \$key + ); + + if ($key) { + my $trimmed_AWSKeyID = substr($key, 4); + my $decoded = decode_base32($trimmed_AWSKeyID); + + my $decoded_prefix = substr($decoded, 0, 6); + my $bigint_value = Math::BigInt -> new('0x' . unpack("H*", $decoded_prefix)); + + my $mask = Math::BigInt -> new('0x7fffffffff80'); + my $accountID = ($bigint_value & $mask) >> 7; + + return $accountID; + } + + if ($help) { + return " + \rCloud::Account_Identifier + \r============== + \r-h, --help See this menu + \r-k, --key Provides the AWS key for extracting the account ID.\n\n"; + } + + return 0; + } +} + +1; \ No newline at end of file