Skip to Content
C# Cookbook
book

C# Cookbook

by Joe Mayo
September 2021
Intermediate to advanced
325 pages
6h 48m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

Chapter 7. Manipulating Data

Every application uses data, and we need to manipulate that data from one form to another. This chapter offers several topics on data transformation in areas such as secret management, JSON serialization, and XML serialization.

Secrets are data, such as passwords or API keys, that we don’t want to expose to third parties. This chapter has three sections on managing secrets for hashing, encryption, and hidden storage.

Much of the data we work with today is in JSON format. Basic serialization/deserialization is simple in modern frameworks, and it’s even simpler when you own both the data consumer and provider. When consuming third-party data, you don’t have control over that data’s consistency or standards. That’s why the JSON sections of this chapter drill down on customizations to help you handle JSON in whatever format you need.

Finally, although JSON has a dominant place among internet data formats today, there’s still plenty of XML data to work with, which is the subject of the XML sections. You’ll see another flavor of LINQ, called LINQ to XML, which gives you full control over the serialization/deserialization process.

7.1 Generating Password Hashes

Problem

You need to securely store user passwords.

Solution

This method generates a random salt to protect secrets:

static byte[] GenerateSalt()
{
    const int SaltLength = 64;

    byte[] salt = new byte[SaltLength];
    var rngRand = new RNGCryptoServiceProvider();

    rngRand.GetBytes(salt);

    return salt;
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 10.0 All-in-One For Dummies

C# 10.0 All-in-One For Dummies

John Paul Mueller
C# 10 Pocket Reference

C# 10 Pocket Reference

Joseph Albahari, Ben Albahari
C# 9.0 in a Nutshell

C# 9.0 in a Nutshell

Joseph Albahari
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 9781492093688Errata PageSupplemental Content