Method: OpenSSL::BN#to_s
- Defined in:
- ossl_bn.c
#to_s(base = 10) ⇒ String
Returns the string representation of the bignum.
BN.new can parse the encoded string to convert back into an OpenSSL::BN.
base
-
The format. Must be one of the following:
-
0
- MPI format. See the man page BN_bn2mpi(3) for details. -
2
- Variable-length and big-endian binary encoding. The sign of the bignum is ignored. -
10
- Decimal number representation, with a leading ‘-’ for a negative bignum. -
16
- Hexadeciaml number representation, with a leading ‘-’ for a negative bignum.
-
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'ossl_bn.c', line 332 static VALUE ossl_bn_to_s(int argc, VALUE *argv, VALUE self) { BIGNUM *bn; VALUE str, bs; int base = 10, len; char *buf; if (rb_scan_args(argc, argv, "01", &bs) == 1) { base = NUM2INT(bs); } GetBN(self, bn); switch (base) { case 0: len = BN_bn2mpi(bn, NULL); str = rb_str_new(0, len); if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len) ossl_raise(eBNError, NULL); break; case 2: len = BN_num_bytes(bn); str = rb_str_new(0, len); if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len) ossl_raise(eBNError, NULL); break; case 10: if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL); str = ossl_buf2str(buf, rb_long2int(strlen(buf))); break; case 16: if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL); str = ossl_buf2str(buf, rb_long2int(strlen(buf))); break; default: ossl_raise(rb_eArgError, "invalid radix %d", base); } return str; } |