@@ -64,7 +64,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
64
64
override NodeBase getChild ( string edgeName ) {
65
65
result = super .getChild ( edgeName )
66
66
or
67
- edgeName = "algorithm " and
67
+ edgeName = "uses " and
68
68
if exists ( this .getAlgorithm ( ) ) then result = this .getAlgorithm ( ) else result = this
69
69
}
70
70
}
@@ -89,13 +89,43 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
89
89
override string getOperationName ( ) { result = "hash" }
90
90
}
91
91
92
+ // Rule: no newtype representing a type of algorithm should be modelled with multiple interfaces
93
+ //
94
+ // Example: HKDF and PKCS12KDF are both key derivation algorithms.
95
+ // However, PKCS12KDF also has a property: the iteration count.
96
+ //
97
+ // If we have HKDF and PKCS12KDF under TKeyDerivationType,
98
+ // someone modelling a library might try to make a generic identification of both of those algorithms.
99
+ //
100
+ // They will therefore not use the specialized type for PKCS12KDF,
101
+ // meaning "from PKCS12KDF algo select algo" will have no results.
102
+ //
103
+ newtype THashType =
104
+ // We're saying by this that all of these have an identical interface / properties / edges
105
+ MD5 ( ) or
106
+ SHA1 ( ) or
107
+ SHA256 ( ) or
108
+ SHA512 ( )
109
+
110
+ class HashAlgorithmType extends THashType {
111
+ string toString ( ) { hashTypeToNameMapping ( this , result ) }
112
+ }
113
+
114
+ predicate hashTypeToNameMapping ( THashType type , string name ) {
115
+ type instanceof SHA1 and name = "SHA-1"
116
+ or
117
+ type instanceof SHA256 and name = "SHA-256"
118
+ or
119
+ type instanceof SHA512 and name = "SHA-512"
120
+ }
121
+
92
122
/**
93
123
* A hashing algorithm that transforms variable-length input into a fixed-size hash value.
94
124
*/
95
- abstract class HashAlgorithm extends Algorithm { }
125
+ abstract class HashAlgorithm extends Algorithm {
126
+ abstract HashAlgorithmType getHashType ( ) ;
96
127
97
- abstract class SHA1 extends HashAlgorithm {
98
- override string getAlgorithmName ( ) { result = "SHA1" }
128
+ override string getAlgorithmName ( ) { hashTypeToNameMapping ( this .getHashType ( ) , result ) }
99
129
}
100
130
101
131
/**
0 commit comments