-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
showed how to encode msgs as Uint8List and how to use Hash-and-Sign-P…
…aradigm
- Loading branch information
1 parent
72fc048
commit f33ac6b
Showing
5 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
import 'dart:math'; | ||
import 'package:crypto/crypto.dart'; | ||
import 'package:dilithium/dilithium.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
main(){ | ||
late DilithiumKeyPair keyPair; | ||
|
||
setUpAll((){ | ||
final random = Random.secure(); | ||
final seed = Uint8List.fromList(List<int>.generate(Dilithium.SEEDBYTES, (_) => random.nextInt(256))); | ||
|
||
keyPair = Dilithium.generateKeyPair(DilithiumParameterSpec.LEVEL3, seed); | ||
}); | ||
|
||
test('Hash and Sign Paradigma', (){ | ||
String msg = "This is a long test message, longer than 32 bytes."; | ||
|
||
// Sender: | ||
Uint8List originalMsg = utf8.encode(msg); | ||
Uint8List hashValue = Uint8List.fromList(sha256.convert(originalMsg).bytes); | ||
|
||
final signature = Dilithium.sign(keyPair.privateKey, hashValue); | ||
|
||
// Receiver: | ||
String receivedMsg = msg; | ||
Uint8List receivedMsgBytes = utf8.encode(receivedMsg); | ||
Uint8List generatedHashValue = Uint8List.fromList(sha256.convert(receivedMsgBytes).bytes); | ||
|
||
bool isValid = Dilithium.verify(keyPair.publicKey, signature, generatedHashValue); | ||
expect(isValid, isTrue); // --> the received message is unmodified | ||
}); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
import 'dart:convert'; | ||
import 'dart:math'; | ||
import 'dart:typed_data'; | ||
import 'package:dilithium/dilithium.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main(){ | ||
late DilithiumKeyPair keyPair; | ||
|
||
setUpAll((){ | ||
final random = Random.secure(); | ||
final seed = Uint8List.fromList(List<int>.generate(Dilithium.SEEDBYTES, (_) => random.nextInt(256))); | ||
|
||
keyPair = Dilithium.generateKeyPair(DilithiumParameterSpec.LEVEL3, seed); | ||
}); | ||
|
||
group('encode message as Uint8List', (){ | ||
test('string msg', () { | ||
String stringMsg = 'Hello World!'; | ||
Uint8List byteMsg = utf8.encode(stringMsg); | ||
|
||
final signature = Dilithium.sign(keyPair.privateKey, byteMsg); | ||
final isValid = Dilithium.verify(keyPair.publicKey, signature, byteMsg); | ||
|
||
expect(isValid, isTrue); | ||
}); | ||
|
||
test('int msg', (){ | ||
ByteData byteData = ByteData(8); // 8 bytes for int (int64) | ||
|
||
int intMsg = 1234567890; | ||
byteData.setInt64(0, intMsg); | ||
Uint8List byteMsg = byteData.buffer.asUint8List(); | ||
|
||
final signature = Dilithium.sign(keyPair.privateKey, byteMsg); | ||
final isValid = Dilithium.verify(keyPair.publicKey, signature, byteMsg); | ||
|
||
expect(isValid, isTrue); | ||
}); | ||
|
||
test('double msg', (){ | ||
ByteData byteData = ByteData(8); // 8 bytes for double | ||
|
||
double doubleMsg = 3.141592653589793; | ||
byteData.setFloat64(0, doubleMsg); | ||
Uint8List byteMsg = byteData.buffer.asUint8List(); | ||
|
||
final signature = Dilithium.sign(keyPair.privateKey, byteMsg); | ||
final isValid = Dilithium.verify(keyPair.publicKey, signature, byteMsg); | ||
|
||
expect(isValid, isTrue); | ||
}); | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ dependencies: | |
dev_dependencies: | ||
lints: ^3.0.0 | ||
test: ^1.24.0 | ||
crypto: ^3.0.0 |