Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example sha256FileHex(). #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Note that IANAC (I am not a cryptologist), so this will surely be flawed in some
Supports certificates signed with RSASSA-PSS!
Still need support for X.500 names (AttributeValueAssertions with well-known object identifiers).
Right now, only common name (2.5.4.3), country (2.5.4.6) and organization (2.5.4.10) are supported.
No specific v3 extensions support yet. Need to implement some standard and well-known types.
No specific v3 extensions support yet. Need to implement some standard and well-known types.
UTCTime is not finished
* Removed erroneous and unnecessary dependency from api to impl module

Expand Down
30 changes: 30 additions & 0 deletions examples/de/dlkw/ccrypto/examples/digest.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import de.dlkw.ccrypto.svc {
sha256,
sha1
}
import import ceylon.file {
Path
}
import ceylon.buffer.base {
base16String
}

"Illustrates the use of SHA-256."
shared void runDigestSha256() {
Expand All @@ -10,6 +16,30 @@ shared void runDigestSha256() {
Byte[] digest = digester.digest({#63.byte});
}

"Given a file, return its sha256sum in hex form."
String? sha256FileHex(Path filePath) {
value digester = sha256();
Byte[] sha256sum;
if (is File file = filePath.resource) {
try (reader = file.Reader()) {
Integer bufferSize = 64 * 1024;
Integer remainingSize = file.size % bufferSize;
variable Integer parts = file.size / bufferSize;
variable Byte[] bytes;
while (parts > 0) {
bytes = reader.readBytes(bufferSize);
digester.update(bytes);
parts--;
}
Byte[] remainder = reader.readBytes(remainingSize);
sha256sum = digester.digest(remainder);
}
return base16String.encode(sha256sum);
} else {
return null;
}
}

"Illustrates the use of SHA-1."
shared void runDigestSha1() {
value digester = sha1();
Expand Down
3 changes: 2 additions & 1 deletion examples/de/dlkw/ccrypto/examples/module.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module de.dlkw.ccrypto.examples "0.0.2" {

// will not be needed once finished, temporary hack to initialize a key pair.
import de.dlkw.ccrypto.impl "0.0.2";

import ceylon.buffer "1.2.2";
import ceylon.file "1.2.2";
import ceylon.random "1.2.2";
}