Can't use HMACSHA1 class in C++ - cryptography

I am trying to follow this sample code for C++, but the line:
Using namespace System::Security::Cryptography;
throws an error in VS 2013 pro. The word System is underlined in red and the mouse-over message says: "name followed by '::' must be a class or namespace name". I have working code using the HMACSHA1 class in several languages (C#, VB.net, python, etc) but can't get it working in C++. What is the problem with accessing this class in C++? Should I be using a different namespace, or adding an #include for some file? I have seen an example of using the HMAC class in C, but that is an "abstract class" and appears that it would require a lot more work to get what I want. I don't understand why I can't use the HMACSHA1 class which delivers exactly what I need.
(Just in case someone asks, my working vb.net code looks like this:
Private Function GetApi_Sig()
Dim api_sig As String = ""
Using myhmac As New HMACSHA1(System.Text.Encoding.UTF8.GetBytes(secret))
Dim hashValue As Byte() = myhmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key))
Dim i As Integer
For i = 0 To (hashValue.Length - 1)
api_sig = api_sig + String.Format("{0:x2}", hashValue(i))
Next i
End Using
Return api_sig
End Function 'GetApi_Sig
)

Related

Workaround for the same namespace defined in two assemblies using VB.NET

The iTextSharp assembly includes the BouncyCastle namespace in the core assembly, but the VB.Net application I am working with uses a different version of BouncyCastle already. I think C# could work around this by using "extern alias" but I can't find how to do this in VB.NET.
Is there any equivalent of extern alias in VB.Net or any other way to exclude specific namespaces from a particular assembly?
You can alias your namespaces in the import like this:
Imports [ aliasname = ] namespace
-or-
Imports [ aliasname = ] namespace.element
IE
Imports strbld = System.Text.StringBuilder
Imports dirinf = System.IO.DirectoryInfo
Public Function GetFolders() As String
Dim sb As New strbld
Dim dInfo As New dirinf("c:\")
For Each dir As dirinf In dInfo.GetDirectories()
sb.Append(dir.Name)
sb.Append(ControlChars.CrLf)
Next
Return sb.ToString
End Function
See MSDN reference for full info: http://msdn.microsoft.com/en-us/library/7f38zh8x.aspx

Can't get Protobuf-net working with vb.net

I recently tried Protobuf-net r668 with my vb.net code. I can mark attributes on my data class but can't get the Serialize and Deserialize features to work.
I followed the instructions at http://code.google.com/p/protobuf-net/wiki/GettingStarted but having converted the code to vb.net I found that this C# code:
using (var file = File.Create("person.bin")) {
Serializer.Serialize(file, person);
}
Won't work when translated to vb.net because the Serialize method does not show up as a method of class Protobuf.Serializer.
Any pointers from anyone who has got Protobuf-net working in vb.net would be helpful.
It should just work; the following runs fine, for example:
Imports System.IO
Module Module1
Sub Main()
Dim strFileName As String = "foo.bin"
Dim f As FileStream = File.Create(strFileName)
Dim objData As Foo = New Foo With {.Name = "abcdef"}
ProtoBuf.Serializer.Serialize(f, objData)
End Sub
<ProtoBuf.ProtoContract>
Class Foo
<ProtoBuf.ProtoMember(1)>
Property Name As String
End Class
End Module
My initial thought is that you have referenced a version of the protobuf-net.dll that is intended for one of the mobile platforms, which expose some features slightly differently. Specifically, a dll from the "core only" build. The intended purpose of each different build is described in the What Files Do I Need.txt file (which is include in the root of the package)

ASP.Net MVC3 VB.Net Custom LabelFor HtmlHelper not working

I am trying to create a custom LabelFor helper to apply by default instead of the standard LabelFor helper include in System.Web.Mvc.Html. I want my LabelFor to take model properties that are PascalCase and make a label that appears as multiple words. For example the property FirstName would appear as "First Name".
I found this post that shows how to make a custom LabelFor helper that allows the addition of html attributes.
I recreated this helper in VB.Net and modified it to do what I want, but I am not able to get it to work.
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Public Module LabelExtensions
<Extension()> _
Public Function LabelFor(Of Tmodel, TValue)(html As HtmlHelper(Of Tmodel), expression As Expression(Of Func(Of Tmodel, TValue))) As MvcHtmlString
Dim metadata As ModelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData)
Dim htmlFieldName As String = ExpressionHelper.GetExpressionText(expression)
Dim labelText As String = If(metadata.DisplayName, If(metadata.PropertyName, htmlFieldName.Split(".").Last()))
If String.IsNullOrEmpty(labelText) Then Return MvcHtmlString.Empty
labelText = Regex.Replace(labelText, ".[A-Z]", Function(m) m.ToString()(0) & " " & Char.ToLower(m.ToString()(1)))
Dim tag As TagBuilder = New TagBuilder("label")
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName))
tag.SetInnerText(labelText)
Return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal))
End Function
The sample includes this Module in Namespace System.Web.Mvc.Html but when I add the namespace declaration to this module everything in the rest of the project goes haywire. For example, each of my models has a Primary Key property that is a Guid datatype and as soon as I add the namespace above to the module, I get several errors stating that System.Guid is not defined among other similar errors.
I've tried to import my project namespace into a view in order to use the custom helper, but then I get an error that says Overload resolution failed because no 'LabelFor' is most specific for these arguments.
I'm stuck. Can anybody help?
I am trying to avoid having to specify a DisplayName for every PascalCase property I have in many models.
I was able to do this successfully if I named my extension method something other than LabelFor. When I named it LabelForPascalCase I was able to use that function in my views. I think the extension method was not overloading the existing method because they had the same signature.

What is wrong here? I'm confused... ToJSON Extension Method

Given the following declaration:
<Extension()> Public Function ToJSON(ByVal target As Object) As String
Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(target.GetType)
Using ms As MemoryStream = New MemoryStream()
serializer.WriteObject(ms, target)
ms.Flush()
Dim bytes As Byte() = ms.ToArray()
Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length)
Return json
End Using
End Function
And the following lines in the Page_Load of a test page:
Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, Object)(
"date", New HttpCookie("woot", "yikes")
)
Put(New HttpCookie("woot", "yikes").ToJSON)
Put(kvp.ToJSON)
Put(kvp.Value.ToJSON)
Put("here".ToJSON())
The first Put works perfect, and puts out the following JSON:
{"Domain":null,"Expires":"\/Date(-62135578800000-0500)\/",
"HttpOnly":false,"Name":"woot","Path":"\/",
"Secure":false,"Value":"yikes"}
The second Put, however, throws a giant, ugly error as so:
Type 'System.Web.HttpCookie' with data contract name 'HttpCookie:http://schemas.datacontract.org/2004/07/System.Web' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
The third Put throws an error also, but COMPLETELY different:
Public member 'ToJSON' on type 'HttpCookie' not found.
And the fourth Put works perfect.
I am very confused why, when the first line works, and the Extension method is clearly being found on the HttpCookie object, why then in the 2nd and 3rd Puts does it NOT work, and why do I get a different error in both cases? Each of the first three Puts is trying to do the same thing - call the ToJSON extension method on the HttpCookie object.
All exposition welcome!
The problem with the third Put is that VB doesn't support extension methods on anything declared to be of type Object: VB.NET: impossible to use Extension method on System.Object instance
This will work: Put(ToJSON(kvp.Value))
And so will this:
Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, HttpCookie)(
"date", New HttpCookie("woot", "yikes"))
Put(kvp.Value.ToJSON)

CruiseControl .Net Plugin Vb.net Error

I am trying to make my own Labeller plugin for Cruise Control .Net 1.4.3. I have made a class based on another plug in example but I keep getting an error
Class 'AssemblyVersionLabeller' must implement 'Function Generate(integrationResult As IIntegrationResult) As String' for interface 'ThoughtWorks.CruiseControl.Core.ILabeller'
Here is my code :
Imports Exortech.NetReflector
Imports ThoughtWorks.CruiseControl.Core
Imports ThoughtWorks.CruiseControl.Core.Util
Namespace NetAssembly.CCNet.Label
_
Public Class AssemblyVersionLabeller
Implements ILabeller
Public Sub Run(ByVal result As IIntegrationResult)
result.Label = Generate(result)
End Sub
Public Function Generate(ByVal integrationResult As IIntegrationResult) As String
Dim label As String = integrationResult.LastIntegration.Label
Return label
End Function
<ReflectorProperty("prefix", Required:=False)> _
Public Prefix As String = String.Empty
End Class
End Namespace
What am I doing wrong? What have I missed?
Background Info:
I am using VS2005. I cant use CrusieControl 1.4.4 RC2 (which has an Assembly Labeller) because my source control's plugin (SCM Anywhere) doesnt work with it.
I cannot judge just by looking at your code, but if you need a sample on how to write labellers (C# code though), you could take a look at BrekiLabeller code (written by me).
I believe you forgot the overrides decleration..
Public Overrides Function Generate