diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml new file mode 100644 index 0000000..4965098 --- /dev/null +++ b/.github/workflows/pr-checks.yml @@ -0,0 +1,18 @@ +# Copyright (c) .NET Foundation and Contributors +# See LICENSE file in the project root for full license information. + +name: PR Checks + +on: + pull_request: + +jobs: + check_package_lock: + name: nanoFramework + uses: nanoframework/nf-tools/.github/workflows/check-package-lock.yml@main + check_nuget_latest: + name: nanoFramework + uses: nanoframework/nf-tools/.github/workflows/check-packages-updated.yml@main + secrets: inherit + with: + solution: 'nanoFramework.System.Security.Cryptography.sln' diff --git a/.github/workflows/update-dependencies.yml b/.github/workflows/update-dependencies.yml new file mode 100644 index 0000000..8df8d19 --- /dev/null +++ b/.github/workflows/update-dependencies.yml @@ -0,0 +1,21 @@ +# Copyright (c) .NET Foundation and Contributors +# See LICENSE file in the project root for full license information. + +# This workflow will periodically check .NET nanoFramework dependencies and updates them in the repository it's running. + +name: Daily update dependencies + +on: + schedule: + # At 00:10 UTC. + - cron: '00 10 * * Mon,Thu' + repository_dispatch: + types: update-dependencies + +jobs: + update-dependencies: + name: nanoFramework + uses: nanoframework/nf-tools/.github/workflows/update-dependencies.yml@main + secrets: inherit + with: + solutionsToCheck: 'nanoFramework.System.Security.Cryptography.sln' diff --git a/.github_changelog_generator b/.github_changelog_generator new file mode 100644 index 0000000..2061cca --- /dev/null +++ b/.github_changelog_generator @@ -0,0 +1,13 @@ +user=nanoframework +project=nanoFramework.System.Security.Cryptography +issues=true +add_issues_wo_labels=false +add_pr_wo_labels=false +add_issues_wo_labels=false +filter_issues_by_milestone=false +exclude_labels=Area: Config-and-Build,Area: Infrastructure-and-Organization,reverted +enhancement_labels=Type: enhancement +bug_labels=Type: bug +merge_prefix=**Documentation and other chores:** +unreleased_label=**Changes available only in 'Preview' NuGet packages:** +author=false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce3e1b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,255 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +#SoundCloud +*.sonarqube/ diff --git a/.runsettings b/.runsettings new file mode 100644 index 0000000..ba4edb1 --- /dev/null +++ b/.runsettings @@ -0,0 +1,17 @@ + + + + + .\TestResults + 120000 + net48 + x64 + + + None + True + COM13 + + + + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..94286c9 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) .NET Foundation and Contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000..ef0bacf --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/README.md b/README.md index 8b782d3..bcd7d3f 100644 --- a/README.md +++ b/README.md @@ -1 +1,40 @@ -# lib-System.Security.Cryptography \ No newline at end of file +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Security.Cryptography&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_System.Security.Cryptography) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Security.Cryptography&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_System.Security.Cryptography) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.System.Security.Cryptography.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Security.Cryptography/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T) + +![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png) + +----- + +### Welcome to the .NET **nanoFramework** System.Security.Cryptography Library repository + +This repository contains the nanoFramework System.Security.Cryptography class library. + +## Build status + +| Component | Build Status | NuGet Package | +|:-|---|---| +| System.Security.Cryptography | [![Build Status](https://dev.azure.com/nanoframework/System.Security.Cryptography/_apis/build/status%2FSystem.Security.Cryptography?branchName=main)](https://dev.azure.com/nanoframework/System.Security.Cryptography/_build/latest?definitionId=68&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Security.Cryptography.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Security.Cryptography/) | + +## System.Security.Cryptography usage + +## Feedback and documentation + +For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home). + +Join our Discord community [here](https://discord.gg/gCyBu8T). + +## Credits + +The list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md). + +## License + +The **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md). + +## Code of Conduct + +This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. +For more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct). + +### .NET Foundation + +This project is supported by the [.NET Foundation](https://dotnetfoundation.org). diff --git a/System.Security.Cryptography/HMACSHA256.cs b/System.Security.Cryptography/HMACSHA256.cs new file mode 100644 index 0000000..2e03871 --- /dev/null +++ b/System.Security.Cryptography/HMACSHA256.cs @@ -0,0 +1,169 @@ +// +// Copyright (c) .NET Foundation and Contributors +// Portions Copyright (c) Microsoft Corporation. All rights reserved. +// See LICENSE file in the project root for full license information. +// + +using System.Runtime.CompilerServices; + +namespace System.Security.Cryptography +{ + /// + /// Computes a Hash-based Message Authentication Code (HMAC) by using the SHA256 hash function. + /// + public class HMACSHA256 : IDisposable + { + private bool _disposed; + private byte[] _keyValue = null; + private byte[] _hashValue; + + /// + /// Gets the value of the computed hash code. + /// + /// The current value of the computed hash code. + /// The object has already been disposed. + public byte[] Hash + { + get + { + if (_disposed) + { + throw new ObjectDisposedException(); + } + + return (byte[])_hashValue.Clone(); + } + } + + /// + /// Gets or sets the key to use in the HMAC calculation. + /// + /// The key to use in the HMAC calculation. + /// + /// + /// This property is the key for the keyed hash algorithm. + /// + /// + /// A Hash-based Message Authentication Code (HMAC) can be used to determine whether a message sent over an insecure channel has been tampered with, provided that the sender and receiver share a secret key. The sender computes the hash value for the original data and sends both the original data and the HMAC as a single message. The receiver recomputes the hash value on the received message and checks that the computed hash value matches the transmitted hash value. + /// + /// + public byte[] Key + { + get + { + return (byte[])_keyValue.Clone(); + } + + set + { + _keyValue = (byte[])value.Clone() ?? throw new ArgumentNullException(); + } + } + + /// + /// Initializes a new instance of the HMACSHA256 class with a randomly generated key. + /// + /// + /// + /// is a type of keyed hash algorithm that is constructed from the SHA-256 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, and then applies the hash function a second time. The output hash is 256 bits in length. + /// + /// + /// This constructor uses a 64-byte, randomly generated key. + /// + /// + public HMACSHA256() + { + // Generate a random key with 64 bytes + Random generator = new(); + _keyValue = new byte[64]; + generator.NextBytes(_keyValue); + } + + + /// + /// Initializes a new instance of the class with the specified key data. + /// + /// The secret key for HMAC computation. The key can be any length. However, the recommended size is 64 bytes. If the key is more than 64 bytes long, it is hashed (using SHA-256) to derive a 32-byte key. + /// The parameter is . + /// + /// is a type of keyed hash algorithm that is constructed from the SHA-256 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, and then applies the hash function a second time. The output hash is 256 bits in length. + /// + public HMACSHA256(byte[] key) + { + _keyValue = key ?? throw new ArgumentNullException(); + } + + /// + /// Computes the hash value for the specified byte array. + /// + /// The input to compute the hash code for. + /// The computed hash code. + /// The object has already been disposed. + /// is . + public byte[] ComputeHash(byte[] buffer) + { + if (_disposed) + { + throw new ObjectDisposedException(); + } + + if (buffer == null) + { + throw new ArgumentNullException(); + } + + _hashValue = HashCore(_keyValue, buffer); + + return (byte[])_hashValue.Clone(); + } + + /// + /// Computes the HMAC of data using the SHA256 algorithm. + /// + /// The HMAC key. + /// The data to HMAC.\ + /// The HMAC of the data. + /// + /// or is . + /// + public static byte[] HashData( + byte[] key, + byte[] source) + { + // key and source are checked for null by HashCore + + return HashCore(key, source); + } + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + protected void Dispose(bool disposing) + { + if (disposing) + { + if (_keyValue != null) + { + Array.Clear(_keyValue, 0, _keyValue.Length); + } + + _keyValue = null; + + // Although we don't have any resources to dispose at this level, + // we need to continue to throw ObjectDisposedExceptions from CalculateHash + // for compatibility with the .NET Framework. + _disposed = true; + } + + return; + } + + [MethodImpl(MethodImplOptions.InternalCall)] + extern private static byte[] HashCore(byte[] key, byte[] source); + } +} diff --git a/System.Security.Cryptography/Properties/AssemblyInfo.cs b/System.Security.Cryptography/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ee37c92 --- /dev/null +++ b/System.Security.Cryptography/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nanoFramework.System.Security.Cryptography")] +[assembly: AssemblyCompany("nanoFramework Contributors")] +[assembly: AssemblyProduct("nanoFramework.System.Security.Cryptography")] +[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +//////////////////////////////////////////////////////////////// +// update this whenever the native assembly signature changes // +[assembly: AssemblyNativeVersion("100.0.0.1")] +//////////////////////////////////////////////////////////////// diff --git a/System.Security.Cryptography/System.Security.Cryptography.nfproj b/System.Security.Cryptography/System.Security.Cryptography.nfproj new file mode 100644 index 0000000..c7e2ca6 --- /dev/null +++ b/System.Security.Cryptography/System.Security.Cryptography.nfproj @@ -0,0 +1,72 @@ + + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 1fe82a4c-bcf5-4195-96c5-6e97cf9a4819 + Library + Properties + 512 + System.Security.Cryptography + nanoFramework.System.Security.Cryptography + v1.0 + True + bin\$(Configuration)\nanoFramework.System.Security.Cryptography.xml + true + true + + + true + + + ..\key.snk + + + false + + + + bin\$(Configuration)\Stubs + nf_sys_sec_cryptography + nanoFramework.System.Security.Cryptography + + + + false + + + + + + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/System.Security.Cryptography/packages.config b/System.Security.Cryptography/packages.config new file mode 100644 index 0000000..75b7f06 --- /dev/null +++ b/System.Security.Cryptography/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/System.Security.Cryptography/packages.lock.json b/System.Security.Cryptography/packages.lock.json new file mode 100644 index 0000000..b80edcb --- /dev/null +++ b/System.Security.Cryptography/packages.lock.json @@ -0,0 +1,19 @@ +{ + "version": 1, + "dependencies": { + ".NETnanoFramework,Version=v1.0": { + "nanoFramework.CoreLibrary": { + "type": "Direct", + "requested": "[1.14.2, 1.14.2]", + "resolved": "1.14.2", + "contentHash": "j1mrz4mitl5LItvmHMsw1aHzCAfvTTgIkRxA0mhs5mSpctJ/BBcuNwua5j3MspfRNKreCQPy/qZy/D9ADLL/PA==" + }, + "Nerdbank.GitVersioning": { + "type": "Direct", + "requested": "[3.6.133, 3.6.133]", + "resolved": "3.6.133", + "contentHash": "VZWMd5YAeDxpjWjAP/X6bAxnRMiEf6tES/ITN0X5CHJgkWLLeHGmEALivmTAfYM6P+P/3Szy6VCITUAkqjcHVw==" + } + } + } +} \ No newline at end of file diff --git a/Tests/System.Security.CryptographyTests/HmacSha256Tests.cs b/Tests/System.Security.CryptographyTests/HmacSha256Tests.cs new file mode 100644 index 0000000..4e6ddf1 --- /dev/null +++ b/Tests/System.Security.CryptographyTests/HmacSha256Tests.cs @@ -0,0 +1,88 @@ +// +// Copyright (c) .NET Foundation and Contributors +// Portions Copyright (c) Microsoft Corporation. All rights reserved. +// See LICENSE file in the project root for full license information. +// + +using nanoFramework.TestFramework; +using System.Collections; +using System.Security.Cryptography; + +namespace System.Security.CryptographyTests +{ + [TestClass] + public class HmacSha256Tests + { + static ArrayList keys = new ArrayList() + { + new byte[] { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }, + new byte[] { 0x4a, 0x65, 0x66, 0x65 }, + new byte[] { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, + new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 }, + new byte[] { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa } + }; + + static ArrayList sources = new ArrayList() + { + new byte[] { 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 }, + new byte[] { 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61, 0x20, 0x77, 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x3f }, + new byte[] { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd }, + new byte[] { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd }, + new byte[] { 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x4b, 0x65, 0x79, 0x20, 0x2d, 0x20, 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79, 0x20, 0x46, 0x69, 0x72, 0x73, 0x74 }, + }; + + static ArrayList expectedHashes = new ArrayList() + { + new byte[] { 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 }, + new byte[] { 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 }, + new byte[] { 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe }, + new byte[] { 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b }, + new byte[] { 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 }, + }; + + [DataRow("RFC4231 Test case 1", 0, 0, 0)] + [DataRow("RFC4231 Test case 2", 1, 1, 1)] + [DataRow("RFC4231 Test case 3", 2, 2, 2)] + [DataRow("RFC4231 Test case 4", 3, 3, 3)] + [DataRow("RFC4231 Test case 6", 4, 4, 4)] + [TestMethod] + public void TestHMACSHA256Static( + string testCase, + int keyIndex, + int sourceIndex, + int hashIndex) + { + OutputHelper.WriteLine($"Test Case: {testCase}"); + + var hash = HMACSHA256.HashData( + (byte[])keys[keyIndex], + (byte[])sources[sourceIndex]); + + CollectionAssert.AreEqual( + (byte[])expectedHashes[hashIndex], + hash); + } + + [DataRow("RFC4231 Test case 1", 0, 0, 0)] + [DataRow("RFC4231 Test case 2", 1, 1, 1)] + [DataRow("RFC4231 Test case 3", 2, 2, 2)] + [DataRow("RFC4231 Test case 4", 3, 3, 3)] + [DataRow("RFC4231 Test case 6", 4, 4, 4)] + [TestMethod] + public void TestHMACSHA256Compute( + string testCase, + int keyIndex, + int sourceIndex, + int hashIndex) + { + OutputHelper.WriteLine($"Test Case: {testCase}"); + + HMACSHA256 hasher = new HMACSHA256((byte[])keys[keyIndex]); + hasher.ComputeHash((byte[])sources[sourceIndex]); + + //CollectionAssert.AreEqual( + // (byte[])expectedHashes[hashIndex], + // hasher.Hash); + } + } +} diff --git a/Tests/System.Security.CryptographyTests/Properties/AssemblyInfo.cs b/Tests/System.Security.CryptographyTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a3735af --- /dev/null +++ b/Tests/System.Security.CryptographyTests/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Tests/System.Security.CryptographyTests/System.Security.CryptographyTests.nfproj b/Tests/System.Security.CryptographyTests/System.Security.CryptographyTests.nfproj new file mode 100644 index 0000000..a92e87a --- /dev/null +++ b/Tests/System.Security.CryptographyTests/System.Security.CryptographyTests.nfproj @@ -0,0 +1,51 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 11add2c9-3053-415e-bb3c-1e0c20f5278e + Library + Properties + 512 + System.Security.CryptographyTests + NFUnitTest + False + true + UnitTest + v1.0 + + + + $(SolutionDir)\.runsettings + + + + + + + + ..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll + + + ..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.TestFramework.dll + + + ..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.UnitTestLauncher.exe + + + + + + + + + + \ No newline at end of file diff --git a/Tests/System.Security.CryptographyTests/packages.config b/Tests/System.Security.CryptographyTests/packages.config new file mode 100644 index 0000000..7704bd8 --- /dev/null +++ b/Tests/System.Security.CryptographyTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Tests/System.Security.CryptographyTests/packages.lock.json b/Tests/System.Security.CryptographyTests/packages.lock.json new file mode 100644 index 0000000..a933330 --- /dev/null +++ b/Tests/System.Security.CryptographyTests/packages.lock.json @@ -0,0 +1,19 @@ +{ + "version": 1, + "dependencies": { + ".NETnanoFramework,Version=v1.0": { + "nanoFramework.CoreLibrary": { + "type": "Direct", + "requested": "[1.14.2, 1.14.2]", + "resolved": "1.14.2", + "contentHash": "j1mrz4mitl5LItvmHMsw1aHzCAfvTTgIkRxA0mhs5mSpctJ/BBcuNwua5j3MspfRNKreCQPy/qZy/D9ADLL/PA==" + }, + "nanoFramework.TestFramework": { + "type": "Direct", + "requested": "[2.1.85, 2.1.85]", + "resolved": "2.1.85", + "contentHash": "UAydT9MgZfufPcLpn5KbFZkR3ul6zSt9ERYDPDen7IWxeT+fGpRTes8QTWfhgMsxF3E27b3wI6K6wwWQkeGuFQ==" + } + } + } +} \ No newline at end of file diff --git a/assets/nf-logo.png b/assets/nf-logo.png new file mode 100644 index 0000000..572c4ff Binary files /dev/null and b/assets/nf-logo.png differ diff --git a/assets/readme.txt b/assets/readme.txt new file mode 100644 index 0000000..7d42e87 --- /dev/null +++ b/assets/readme.txt @@ -0,0 +1,21 @@ + _____ _ + _ __ __ _ _ __ ___ | ___| __ __ _ _ __ ___ _____ _____ _ __| | __ + | '_ \ / _` | '_ \ / _ \| |_ | '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ / + | | | | (_| | | | | (_) | _|| | | (_| | | | | | | __/\ V V / (_) | | | < + |_| |_|\__,_|_| |_|\___/|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\ + +=================================================================================== + +API docs: https://docs.nanoframework.net/api/nanoFramework.System.Security.Cryptography.html + +Browse our samples repository: https://github.com/nanoframework/samples + +Check our documentation online: https://docs.nanoframework.net/ + +Join our lively Discord community: https://discord.gg/gCyBu8T + +Report issues: https://github.com/nanoframework/Home/issues + +Follow us on Twitter: https://twitter.com/nanoframework + +Follow our YouTube channel: https://www.youtube.com/c/nanoFramework diff --git a/azure-pipelines.yml b/azure-pipelines.yml index af93855..913bf92 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,19 +1,59 @@ -# Starter pipeline -# Start with a minimal pipeline that you can customize to build and deploy your code. -# Add steps that build, run tests, deploy, and more: -# https://aka.ms/yaml +# Copyright (c) .NET Foundation and Contributors +# See LICENSE file in the project root for full license information. trigger: -- develop + branches: + include: + - main + - release-* + paths: + exclude: + - .github_changelog_generator + - .gitignore + - CHANGELOG.md + - LICENSE.md + - README.md + - NuGet.Config + - assets/* + - config/* + - .github/* + +# PR always trigger build +pr: + autoCancel: true + +# add nf-tools repo to resources (for Azure Pipelines templates) +resources: + repositories: + - repository: templates + type: github + name: nanoframework/nf-tools + endpoint: nanoframework pool: - vmImage: ubuntu-latest + vmImage: 'windows-latest' + +variables: + DOTNET_NOLOGO: true + solution: 'nanoFramework.System.Security.Cryptography.sln' + buildPlatform: 'Any CPU' + buildConfiguration: 'Release' + nugetPackageName: 'nanoFramework.System.Security.Cryptography' steps: -- script: echo Hello, world! - displayName: 'Run a one-line script' -- script: | - echo Add other tasks to build, test, and deploy your project. - echo See https://aka.ms/yaml - displayName: 'Run a multi-line script' +# step from template @ nf-tools repo +# all build, update and publish steps +- template: azure-pipelines-templates/class-lib-build.yml@templates + parameters: + sonarCloudProject: 'nanoframework_System.Security.Cryptography' + runUnitTests: false + unitTestRunsettings: '$(System.DefaultWorkingDirectory)\.runsettings' + +# step from template @ nf-tools repo +# report error +- template: azure-pipelines-templates/discord-webhook-task.yml@templates + parameters: + status: 'failure' + webhookUrl: '$(DiscordWebhook)' + message: '' diff --git a/config/SignClient.json b/config/SignClient.json new file mode 100644 index 0000000..482177d --- /dev/null +++ b/config/SignClient.json @@ -0,0 +1,14 @@ +{ + "SignClient": { + "AzureAd": { + "AADInstance": "https://login.microsoftonline.com/", + "ClientId": "c248d68a-ba6f-4aa9-8a68-71fe872063f8", + "TenantId": "16076fdc-fcc1-4a15-b1ca-32c9a255900e" + }, + "Service": { + "Url": "https://codesign.dotnetfoundation.org/", + "ResourceId": "https://SignService/3c30251f-36f3-490b-a955-520addb85001" + } + } + } + \ No newline at end of file diff --git a/config/filelist.txt b/config/filelist.txt new file mode 100644 index 0000000..bf9e1c2 --- /dev/null +++ b/config/filelist.txt @@ -0,0 +1 @@ +**/nanoFramework.System.Security.Cryptography.* \ No newline at end of file diff --git a/key.snk b/key.snk new file mode 100644 index 0000000..67c9bb0 Binary files /dev/null and b/key.snk differ diff --git a/nanoFramework.System.Security.Cryptography.nuspec b/nanoFramework.System.Security.Cryptography.nuspec new file mode 100644 index 0000000..31d12c8 --- /dev/null +++ b/nanoFramework.System.Security.Cryptography.nuspec @@ -0,0 +1,37 @@ + + + + nanoFramework.System.Security.Cryptography + $version$ + nanoFramework.System.Security.Cryptography + nanoframework + false + LICENSE.md + + + docs\README.md + false + https://github.com/nanoframework/System.Security.Cryptography + images\nf-logo.png + + Copyright (c) .NET Foundation and Contributors + This package includes the nanoFramework System.Security.Cryptography assembly for .NET nanoFramework C# projects. +This package requires a target with nanoFramework.System.Security.Cryptography v$nativeVersion$ (checksum $checksum$). + nanoFramework C# csharp netmf netnf + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nanoFramework.System.Security.Cryptography.sln b/nanoFramework.System.Security.Cryptography.sln new file mode 100644 index 0000000..4fb799a --- /dev/null +++ b/nanoFramework.System.Security.Cryptography.sln @@ -0,0 +1,47 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "System.Security.Cryptography", "System.Security.Cryptography\System.Security.Cryptography.nfproj", "{1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{7B20F2BC-2DD3-42C3-AE9B-01F5F558ADFE}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{49C02042-2EA7-4EC8-B341-473EC5CBE2BE}" + ProjectSection(SolutionItems) = preProject + .runsettings = .runsettings + NuGet.Config = NuGet.Config + version.json = version.json + EndProjectSection +EndProject +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "System.Security.CryptographyTests", "Tests\System.Security.CryptographyTests\System.Security.CryptographyTests.nfproj", "{11ADD2C9-3053-415E-BB3C-1E0C20F5278E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Release|Any CPU.Build.0 = Release|Any CPU + {1FE82A4C-BCF5-4195-96C5-6E97CF9A4819}.Release|Any CPU.Deploy.0 = Release|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Release|Any CPU.Build.0 = Release|Any CPU + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E}.Release|Any CPU.Deploy.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {11ADD2C9-3053-415E-BB3C-1E0C20F5278E} = {7B20F2BC-2DD3-42C3-AE9B-01F5F558ADFE} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FBF8F1F8-8AB4-4305-8E5D-E8E20849D22B} + EndGlobalSection +EndGlobal diff --git a/version.json b/version.json new file mode 100644 index 0000000..fdf769d --- /dev/null +++ b/version.json @@ -0,0 +1,24 @@ +{ + "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", + "version": "1.0", + "assemblyVersion": { + "precision": "build" + }, + "semVer1NumericIdentifierPadding": 3, + "nuGetPackageVersion": { + "semVer": 2.0 + }, + "publicReleaseRefSpec": [ + "^refs/heads/develop$", + "^refs/heads/main$", + "^refs/heads/v\\d+(?:\\.\\d+)?$" + ], + "cloudBuild": { + "setAllVariables": true + }, + "release": { + "branchName": "release-v{version}", + "versionIncrement": "build", + "firstUnstableTag": "preview" + } +}