Chromiumでmultipartのboundaryを生成していると思われる箇所
Google Chromeなどを使って、multipartで送ったときにどうやって------WebKitFormBoundaryfYi1UpulVXBlAeCDのような文字列を生成しているか、特にランダムに見れる文字列の生成方法が気になっていた。 以下が、該当箇所だと思われる。
code:third_party/blink/renderer/platform/network/form_data_encoder.cc
...
Vector<char> FormDataEncoder::GenerateUniqueBoundaryString() {
Vector<char> boundary;
// TODO(rsleevi): crbug.com/575779: Follow the spec or fix the spec.
// The RFC 2046 spec says the alphanumeric characters plus the
// following characters are legal for boundaries: '()+_,-./:=?
// However the following characters, though legal, cause some sites
// to fail: (),./:=+
//
// Note that our algorithm makes it twice as much likely for 'A' or 'B'
// to appear in the boundary string, because 0x41 and 0x42 are present in
// the below array twice.
static const char kAlphaNumericEncodingMap64 = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B,
0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42};
// Start with an informative prefix.
Append(boundary, "----WebKitFormBoundary");
// Append 16 random 7bit ascii AlphaNumeric characters.
base::RandBytes(random_bytes, sizeof(random_bytes));
for (char& c : random_bytes)
boundary.Append(random_bytes, sizeof(random_bytes));
boundary.push_back(
0); // Add a 0 at the end so we can use this as a C-style string.
return boundary;
}
...
一見、大文字小文字アルファベットと数字の乱数のように見えるが、
上記のコード内のコメント欄で触れられているように、"A"と"B"の出現確率が2倍になっている。
// Note that our algorithm makes it twice as much likely for 'A' or 'B'
// to appear in the boundary string, because 0x41 and 0x42 are present in
// the below array twice.
このコメントどおり、kAlphaNumericEncodingMapには"A", "B"が多く含まれている。
なぜ、2倍の確率なのかは、どこかのissueなどを探せば見つかるかも。