Skip to content

Commit

Permalink
remove dependency with Cryptography (by just copying the only used cl…
Browse files Browse the repository at this point in the history
…ass into our own package)

remove unused packages
  • Loading branch information
estebanlm committed Jun 15, 2023
1 parent c66db64 commit ad07ac0
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 40 deletions.
47 changes: 9 additions & 38 deletions mc/BaselineOfMongoTalk/BaselineOfMongoTalk.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,20 @@ Class {
#category : #BaselineOfMongoTalk
}

{ #category : #'external projects' }
BaselineOfMongoTalk >> PBKDF2: spec [
spec
baseline: 'Cryptography' with: [
spec
repository: 'github://pharo-contributions/Cryptography:0.5/source' ];

project: 'PBKDF2' copyFrom: 'Cryptography' with: [
spec loads: 'PBKDF2' ]
]

{ #category : #baseline }
BaselineOfMongoTalk >> baseline: spec [
<baseline>
spec
for: #common
do: [

self PBKDF2: spec.
self osSubprocess: spec.
do: [

spec
"Core and Client, without any test."
package: 'Mongo-Pharo11';
package: 'Mongo-BSON';
package: 'Mongo-Queries';
package: 'Mongo-Squeak' with: [ spec requires: #('Mongo-BSON') ];
package: 'Mongo-Core' with: [ spec requires: #('PBKDF2' 'Mongo-Squeak' 'Mongo-BSON') ];
package: 'Mongo-Core' with: [ spec requires: #('Mongo-Squeak' 'Mongo-BSON') ];
package: 'Mongo-DriverV5' with: [ spec requires: #('Mongo-Core') ];
package: 'Mongo-DriverLegacy' with: [ spec requires: #('Mongo-Core') ];
package: 'Mongo-Client' with: [ spec requires: #('Mongo-Core') ];
Expand All @@ -40,20 +26,21 @@ BaselineOfMongoTalk >> baseline: spec [
package: 'Mongo-Tests-Queries' with: [ spec requires: #('Mongo-Queries') ];
package: 'Mongo-Tests-BSON' with: [ spec requires: #('Mongo-BSON') ];
package: 'Mongo-Tests-Core' with: [ spec requires: #('Mongo-Core') ];
package: 'Mongo-Client-Tests' with: [ spec requires: #('Mongo-Tests-Core' 'Mongo-Client') ];
package: 'Mongo-Client-Tests' with: [ spec requires: #('Mongo-Tests-Core' 'Mongo-Client') ].
"Tests for Replica Set."
package: 'Mongo-Tests-ReplicaSet' with: [ spec requires: #('Mongo-Core' 'OSSubprocess') ];
"package: 'Mongo-Tests-ReplicaSet' with: [
spec requires: #('Mongo-Core' 'OSSubprocess') ];
package: 'Mongo-Client-ReplicaSet-Tests' with: [
spec requires: #('Mongo-Tests-ReplicaSet' 'Mongo-Client-Tests') ].
spec requires: #('Mongo-Tests-ReplicaSet' 'Mongo-Client-Tests') ]."

spec
group: 'default' with: #('Core' 'Tests');
group: 'Core' with: #('Mongo-Core' 'Mongo-Queries' 'Mongo-DriverV5');
group: 'Tools' with: #('Mongo-Pharo-Tools');
group: 'Tests' with: #('Core' 'Mongo-Tests-BSON' 'Mongo-Tests-Core' 'Mongo-Tests-Queries');
group: 'Tests + ReplicaSet Tests' with: #(Tests 'Mongo-Tests-ReplicaSet');
group: 'Client' with: #('default' 'Mongo-Client' 'Mongo-Client-Tests');
group: 'Client + ReplicaSet Tests' with: #('Client' 'Mongo-Client-ReplicaSet-Tests') ].
"group: 'Tests + ReplicaSet Tests' with: #(Tests 'Mongo-Tests-ReplicaSet');"
group: 'Client' with: #('default' 'Mongo-Client' 'Mongo-Client-Tests') ].
"group: 'Client + ReplicaSet Tests' with: #('Client' 'Mongo-Client-ReplicaSet-Tests') ]".

spec
for: #(#'pharo10.x' #'pharo11.x' #'pharo12.x')
Expand All @@ -64,22 +51,6 @@ BaselineOfMongoTalk >> baseline: spec [
do: [ spec package: 'Mongo-BSON' with: [ spec requires: #('Mongo-Pharo11') ] ]
]

{ #category : #'external projects' }
BaselineOfMongoTalk >> grease: spec [
spec
baseline: 'Grease' with: [
spec
repository: 'github://SeasideSt/Grease:v1.5.x/repository' ]

]

{ #category : #'external projects' }
BaselineOfMongoTalk >> osSubprocess: spec [
spec
baseline: 'OSSubprocess'
with: [ spec repository: 'github://pharo-contributions/OSSubprocess:v1.3.0/repository' ]
]

{ #category : #'external projects' }
BaselineOfMongoTalk >> taskIt: spec [
spec baseline: 'TaskIt' with: [
Expand Down
181 changes: 181 additions & 0 deletions mc/Mongo-Core/MongoPBKDF2.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
"
This class is taken from the Cryptography package, kudos to their original authors.
PBKDF2 implementation based on RFC 2898 (http://tools.ietf.org/html/rfc2898).
Example:
```
derivedKey := PBKDF2 new
hashFunction: SHA1;
password: 'password';
salt: 'salt';
iterations: 4096;
length: 256;
deriveKey.
```
You can also use some convenience class methods. E.g.:
```
derivedKey := PBKDF2 derivedKeySHA1Password: password salt: salt.`
```
## Defaults:
- prf: HMAC-SHA-1
- iterations: 1000
- length: 16 Bytes
"
Class {
#name : #MongoPBKDF2,
#superclass : #Object,
#instVars : [
'prf',
'hLen',
'p',
's',
'c',
'dkLen'
],
#category : #'Mongo-Core-Auth'
}

{ #category : #'instance creation' }
MongoPBKDF2 class >> derivedKeyHashFunction: hashClass password: password salt: salt [
^ PBKDF2 new
hashFunction: hashClass;
password: password;
salt: salt;
deriveKey
]

{ #category : #'instance creation' }
MongoPBKDF2 class >> derivedKeyHashFunction: hashClass password: password salt: salt iterations: iterations length: length [
^ PBKDF2 new
hashFunction: hashClass;
password: password;
salt: salt;
iterations: iterations;
length: length;
deriveKey
]

{ #category : #'instance creation' }
MongoPBKDF2 class >> derivedKeyPseudoRandomFunction: prf password: password salt: salt iterations: iterations length: length [
^ PBKDF2 new
prf: prf;
password: password;
salt: salt;
iterations: iterations;
length: length;
deriveKey
]

{ #category : #'instance creation' }
MongoPBKDF2 class >> derivedKeySHA1Password: password salt: salt [
^ PBKDF2 new
password: password;
salt: salt;
deriveKey
]

{ #category : #accessing }
MongoPBKDF2 >> c: anInteger [
c := anInteger
]

{ #category : #accessing }
MongoPBKDF2 >> deriveBlock: i [
| u un |
un := prf
key: p;
digestMessage: s , (i asByteArrayOfSize: 4).
u := un.
c - 1
timesRepeat: [
un := prf digestMessage: un.
u := u bitXor: un ].
^ u
]

{ #category : #accessing }
MongoPBKDF2 >> deriveKey [
| l |
dkLen > (((2 raisedTo: 32) - 1) * hLen)
ifTrue: [ self error: 'derived key too long' ]. "If dkLen > (2^32 - 1) * hLen, output 'derived key too long' and
stop."
l := (dkLen / hLen) ceiling. "Let l be the number of hLen-octet blocks in the derived key,
rounding up" "r := dkLen - ((l - 1) * hLen)." "and let r be the number of octets in the last
block:"
^ (ByteArray streamContents: [ :dkStream | 1 to: l do: [ :i | dkStream nextPutAll: (self deriveBlock: i) ] ]) contents
copyFrom: 1
to: dkLen
]

{ #category : #accessing }
MongoPBKDF2 >> dkLen: anInteger [
dkLen := anInteger
]

{ #category : #accessing }
MongoPBKDF2 >> hashFunction: anHashFunction [
self prf: anHashFunction new hmac
]

{ #category : #initialization }
MongoPBKDF2 >> initialize [
self
hashFunction: SHA1;
iterations: 1000;
length: 16
]

{ #category : #accessing }
MongoPBKDF2 >> iterations: anInteger [
self c: anInteger
]

{ #category : #accessing }
MongoPBKDF2 >> length: anInteger [
self dkLen: anInteger
]

{ #category : #accessing }
MongoPBKDF2 >> p: aByteArray [
p := aByteArray
]

{ #category : #accessing }
MongoPBKDF2 >> password: aString [
self p: aString asByteArray
]

{ #category : #accessing }
MongoPBKDF2 >> prf: aPseudoRandomFunction [
prf := aPseudoRandomFunction.
hLen := self prfHLen: aPseudoRandomFunction
]

{ #category : #accessing }
MongoPBKDF2 >> prfHLen: aPseudoRandomFunction [
^ aPseudoRandomFunction digestSize
]

{ #category : #accessing }
MongoPBKDF2 >> prfKey: k message: m [
^ prf
key: k;
digestMessage: m
]

{ #category : #accessing }
MongoPBKDF2 >> s: aByteArray [
s := aByteArray
]

{ #category : #accessing }
MongoPBKDF2 >> salt: aString [
self s: aString asByteArray
]
4 changes: 2 additions & 2 deletions mc/Mongo-Core/SCRAMAuthMechanism.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ I implement SCRAM-SHA1 auth mechanism
Class {
#name : #SCRAMAuthMechanism,
#superclass : #MongoAuthMechanism,
#category : 'Mongo-Core-Auth'
#category : #'Mongo-Core-Auth'
}

{ #category : #testing }
Expand Down Expand Up @@ -34,7 +34,7 @@ SCRAMAuthMechanism >> authenticateUsername: username password: password forcedDa
(rnonce beginsWith: nonce)
ifFalse: [ ^ false "Server returned an invalid nonce." ].
without_proof := 'c=biws,r=' , rnonce.
derivedKey := PBKDF2 new
derivedKey := MongoPBKDF2 new
hashFunction: self hashFunction;
password: (self digestUsername: username password: password);
salt: salt base64Decoded;
Expand Down

0 comments on commit ad07ac0

Please sign in to comment.