From 5c71ce4f7e273879ec3a5f030ead6b08c6598813 Mon Sep 17 00:00:00 2001 From: bhavit04 Date: Mon, 27 Jul 2026 13:08:18 +0530 Subject: [PATCH] docs: move PBKDF2 example to a separate file (#283) Moves the Pbkdf2SecretKey example out of the doc comment into example/webcrypto/pbkdf2/derive_bits.dart and references it with {@example}, like the HMAC one. Also fixes bugs the move surfaced: utf8.decode should be utf8.encode for the password and salt, and deriveBits is an instance method so it's called on the key. --- example/webcrypto/pbkdf2/derive_bits.dart | 41 +++++++++++++++++++++++ lib/src/webcrypto/webcrypto.pbkdf2.dart | 22 +----------- 2 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 example/webcrypto/pbkdf2/derive_bits.dart diff --git a/example/webcrypto/pbkdf2/derive_bits.dart b/example/webcrypto/pbkdf2/derive_bits.dart new file mode 100644 index 00000000..bcddbe1b --- /dev/null +++ b/example/webcrypto/pbkdf2/derive_bits.dart @@ -0,0 +1,41 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ignore_for_file: avoid_print + +// #region example +import 'dart:convert' show utf8, base64; + +import 'package:webcrypto/webcrypto.dart'; + +Future main() async { + // Provide a password to be used for key derivation. + final key = await Pbkdf2SecretKey.importRawKey( + utf8.encode('my-password-in-plain-text'), + ); + + // Derive a key from the password. + final derivedKey = await key.deriveBits( + 256, // number of bits to derive. + Hash.sha256, + utf8.encode('unique salt'), + 100000, + ); + + // Print the derived key. This could also be used as the basis for other + // new symmetric cryptographic keys. + print(base64.encode(derivedKey)); +} + +// #endregion diff --git a/lib/src/webcrypto/webcrypto.pbkdf2.dart b/lib/src/webcrypto/webcrypto.pbkdf2.dart index 37fcc46e..8138331d 100644 --- a/lib/src/webcrypto/webcrypto.pbkdf2.dart +++ b/lib/src/webcrypto/webcrypto.pbkdf2.dart @@ -25,27 +25,7 @@ part of 'webcrypto.dart'; /// /// {@template Pbkdf2SecretKey:example} /// **Example** -/// ``` -/// import 'dart:convert' show utf8, base64; -/// import 'package:webcrypto/webcrypto.dart'; -/// -/// // Provide a password to be used for key derivation -/// final key = await Pbkdf2SecretKey.importRawKey(utf8.decode( -/// 'my-password-in-plain-text', -/// )); -/// -/// // Derive a key from password -/// final derivedKey = await Pbkdf2SecretKey.deriveBits( -/// 256, // number of bits to derive. -/// Hash.sha256, -/// utf8.decode('unique salt'), -/// 100000, -/// ); -/// -/// // Print the derived key, this could also be used as basis for other new -/// // symmetric cryptographic keys. -/// print(base64.encode(derivedKey)); -/// ``` +/// {@example /example/webcrypto/pbkdf2/derive_bits.dart#example} /// {@endtemplate} /// /// [1]: https://www.rfc-editor.org/rfc/rfc8018